Hi,
When i tried to import the data's from excel and then upload to database, I am getting error as below:even though the selected file is closed, i think while browsing the files it shows the preview, becoz of that this would may happen, kindly solve this..
The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.
My code as fallows:
string sFilePath = "";
sFilePath = FileUpload1.PostedFile.FileName;
string sSourceConstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + sFilePath + "; Extended Properties=" + "\"Excel 8.0;HDR=YES;+\"";
// string sSourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\sathya\Desktop\Employee.xls; Extended Properties=""Excel 8.0;HDR=YES;""";
string sDestConstr = ConfigurationManager.ConnectionStrings["setting"].ConnectionString;
OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
using (sSourceConnection)
{
string sql = string.Format("Select [EmpId],[F_Name],[L_Name],[City],[EmailId],[EmpJoining] FROM [{0}]", "Sheet1$");
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
using (OleDbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sDestConstr))
{
bulkCopy.DestinationTableName = "Employee";
//You can mannualy set the column mapping by the following way.
bulkCopy.ColumnMappings.Add("EmpId", "EmpId");
bulkCopy.ColumnMappings.Add("F_Name", "F_Name");
bulkCopy.ColumnMappings.Add("L_Name", "L_Name");
bulkCopy.ColumnMappings.Add("City", "City");
bulkCopy.ColumnMappings.Add("EmailId", "EmailId");
bulkCopy.ColumnMappings.Add("EmpJoining", "EmpJoining");
bulkCopy.WriteToServer(dr);
}
}
}
Sathish Kumar S