我正在使用SqlBulkCopy
将Excel文件导入SQL Server。
但是每次我都收到不同的错误,例如
无法更新。数据库或对象是只读的。
或
找不到可安装的 ISAM。
或
Microsoft Jet 数据库引擎找不到该对象。确保对象存在,并且正确拼写其名称和路径名。
检查下面的代码...
String strConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filepath+";Extended Properties=Excel 8.0;HDR=YES;";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand
("Select * from [Sheet1$]",
excelConnection);
MessageBox.Show("ss");
excelConnection.Open();
MessageBox.Show("ss2");
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
MessageBox.Show("ss1");
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "RecExcelTable";
//sqlBulk.ColumnMappings.Add("ID", "ID");
//sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.WriteToServer(dReader);
此外,我还需要将此代码兼容为任何版本的 Excel。
我该怎么做并解决我的错误?
我有类似的问题。 检查文件路径是否正确
喜欢
C:\file.xls
作为测试。
并检查版本是否正确。 8.0 很旧 我认为这就像办公室 2000,不是吗?