protected void btnsave_Click(object sender, EventArgs e)
{
if (FileUpload.HasFile)
{
importDataFromExcel(FileUpload.PostedFile.FileName);
}
}
public void importDataFromExcel(string excelFilePath)
{
string sSQLTable = "dbo.Import_SFS";
string myExcelDataQuery = "Select Brand,Region_ID,Account_Name from [Data$]";
try
{
string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
string sSqlConnectionString = "Data Source=localhost;Initial Catalog=MDFW;Integrated Security=True";
string sClearSQL = "DELETE FROM " + sSQLTable;
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
bulkCopy.DestinationTableName = sSQLTable;
while (dr.Read())
{
bulkCopy.WriteToServer(dr);
}
OleDbConn.Close();
}
catch (Exception ex)
{
}
}
this is the solution of my problem.
i did RND with your given links.and my ownself also search.and finaly i solve it.
Thanks