【发布时间】:2015-03-08 04:39:36
【问题描述】:
我正在创建一个小应用程序来将 excel 数据导入我的数据库,当我单击按钮时它会因错误而崩溃
外部表不是预期的格式。
我尝试用谷歌搜索并在这里和那里更改代码,但问题仍然存在。我尝试将文件保存为 .xls,当我运行代码时,页面因 google chrome 的此网页不可用(甚至无法进入调试)而脱机
这是我的代码:
string strConnection = ConfigurationManager.ConnectionStrings["---"].ConnectionString;
String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
//Create Connection to Excel work book
using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
{
//Create OleDbCommand to fetch data from Excel
using (OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection))
{
excelConnection.Open();
using (OleDbDataReader dReader = cmd.ExecuteReader())
{
using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
{
//Give your Destination table name
sqlBulk.DestinationTableName = "TableName";
sqlBulk.WriteToServer(dReader);
}
}
}
}
【问题讨论】:
-
我已经访问了两个链接,链接中的连接字符串与我使用 ACE.OLEDB 的连接字符串相同,但仍然无法解决我的问题
-
我假设您已经安装了 Microsoft Access 2010 Runtime! microsoft.com/en-us/download/details.aspx?id=10910