c# - 如何在 C# 中修复“System.Data.dll 中未处理的异常‘System.Runtime.InteropServices.SEHException’”
问题描述
我遇到了一个关于我bulk insert
的XLSM (excel file)
麻烦 SQL Database
。规范,我可以从bulk insert
没有麻烦,一切都很好。但是当我unistalled 2010 替换为版本时。我的项目有一个excel file 2010
SQL database
excel 2016
“抛出异常:System.Data.dll 中的‘System.Runtime.InteropServices.SEHException’”
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper& sessionWrapper)
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at WpfApplication5.MainWindow.Laygia() in G:\WpfApplication5 - Copy (8)-20181003T095641Z-001\WpfApplication5 - Copy (8)\WpfApplication5\MainWindow.xaml.cs:line 2463
这是我的代码:
using (FileStream fs = new FileStream(linkpath0, FileMode.Open))
{
using (StreamReader rd = new StreamReader(fs, Encoding.UTF8))
{// linkpath = C:\Users\Win8.1 VS10 X64\Google Drive
String linkpath = rd.ReadToEnd();
string path1 = linkpath + @"\xBanggia\xBanggia.xlsm";
string excelConnectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0 Xml", path1);
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("SELECT * FROM[Sheet$A0:L]", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = @"Data Source=.;Initial Catalog=DemoPricetable;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "Infobanggia";
bulkCopy.WriteToServer(dr);
// MessageBox.Show("!!!!!!!!!!!!!!The data has been exported succefuly from Excel to SQL");
}
connection.Close();
}
}
}
}
当我通过异常调试它时:
OleDbCommand command = new OleDbCommand
("SELECT * FROM[Sheet$A0:L]", connection);:
这是我尝试过的:
我尝试改变library
_Office
Excel
14.0->16.0
仅使用一个 excel 文件,但使用excel
(2010 和 2016)的单独版本
我来宾说这个问题与upgrading
excel 版本有关
非常感谢!!!!