OleDbDataReader 对象无法读取文件
Posted
技术标签:
【中文标题】OleDbDataReader 对象无法读取文件【英文标题】:OleDbDataReader object cannot read the file 【发布时间】:2014-10-15 12:05:38 【问题描述】:我开发了一个功能,将 Excel 工作表导入表格。但是当我上传文件并单击按钮导入时。该代码不起作用,并给了我下面提到的错误。我尝试使用 DataAdapter,但这也不起作用。请看下面的错误:-
Microsoft Office Access 数据库引擎找不到对象 '
TableName
'。确保对象存在并且您拼写它 名称和路径名正确。
另外,请参阅代码以供参考:-
private void ImporttoSQL(string sPath)
string sSourceConstr1 = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\AgentList.xls; Extended Properties=""Excel 8.0;HDR=YES;""", sPath);
string sSourceConstr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=0;Extended Properties=""Excel 12.0 Xml;HDR=YES;""", sPath);
OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
using (sSourceConnection)
string sql = string.Format("Select [Merchant_Name],[Store_Name],[Store_Address],[City] FROM [MerchantTempDetail]", "Sheet1$");
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
conn.Open();
using (OleDbDataReader dr = command.ExecuteReader())
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
bulkCopy.DestinationTableName = "MerchantTempDetail";
//You can mannualy set the column mapping by the following way.
// bulkCopy.ColumnMappings.Add("Mini_Category_Id", "Mini_Category_Id");
//bulkCopy.ColumnMappings.Add("CategoryId", "CategoryId");
bulkCopy.ColumnMappings.Add("Merchant_Name", "Merchant_Name");
bulkCopy.ColumnMappings.Add("Store_Name", "Store_Name");
bulkCopy.ColumnMappings.Add("Store_Address", "Store_Address");
bulkCopy.ColumnMappings.Add("City", "City");
bulkCopy.WriteToServer(dr);
编辑代码:-
public static void ExcelToSqlServerBulkCopy()
// Connection String to Excel Workbook
// Jet4
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=AgentList.xls; Extended Properties=""Excel 8.0;HDR=YES;""";
// Ace Ole db 12
string excelAceOleDb12ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=AgentList.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelAceOleDb12ConnectionString))
OleDbCommand command = new OleDbCommand("Select [Merchant_Name],[Store_Name],[Store_Address],[City] FROM [Sheet1$]", connection);
// open excel
connection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
// SQL Server Connection String
string sqlConnectionString = ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString;
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
bulkCopy.DestinationTableName = "MerchantTempDetail";
bulkCopy.WriteToServer(dr);
protected void btnImport_Click(object sender, EventArgs e)
if (FileUpload1.HasFile)
string sPath = Server.MapPath(FileUpload1.FileName);
FileUpload1.SaveAs(sPath);
ExcelToSqlServerBulkCopy();
【问题讨论】:
您的SqlBulkCopy
在与conn
连接的数据库中搜索MerchantTempDetail
。但我没有看到它的定义。我只将sSourceConnection
视为OleDbConnection
,它连接在您的sPath
路径中。
所以,你的意思是说,我应该叫 (dr) 代替 (conn) 工作?
【参考方案1】:
您需要定义 2 个连接,一个用于 Excel 源,另一个用于 sql 数据库您要批量复制到。 (此代码已测试)如果您需要复制到内存数据表,请使用此example on MSDN。
public static void ExcelToSqlServerBulkCopy ()
// Connection String to Excel Workbook
// Jet4
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=AgentList.xls; Extended Properties=""Excel 8.0;HDR=YES;""";
// Ace Ole db 12
string excelAceOleDb12ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=AgentList.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelAceOleDb12ConnectionString))
OleDbCommand command = new OleDbCommand("Select [Merchant_Name],[Store_Name],[Store_Address],[City] FROM [AgentList$]", connection);
// open excel
connection.Open ();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader ())
// SQL Server Connection String
string sqlConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=***;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy (sqlConnectionString))
bulkCopy.DestinationTableName = "Q26382169";
bulkCopy.WriteToServer (dr);
上面使用变量名的非常重要的细节:
-
必须先创建要复制到的数据库 (***)。
目标表 (Q26382169) 必须存在于该数据库中,DDL 无关紧要,与 Excel 文件的列相同。
由于您使用的是 HDR=YES 选项,因此 Excel 工作表的第一行必须包含指定的列名:Merchant_Name、Store_Name、Store_Address、City(不带 [])
OleDbCommand (AgentList$) 中工作表的名称
【讨论】:
我添加了我的更改,但它给了我这样的错误:“Microsoft Office Access 数据库引擎无法打开或写入文件''。它已经被另一个用户以独占方式打开,或者您需要获得查看和写入其数据的权限。”在线:connection.Open();查看我的更新代码【参考方案2】:终于通过调试解决了。
private void ImporttoSQL(string sPath)
string sSourceConstr1 = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\AgentList.xls; Extended Properties=""Excel 8.0;HDR=YES;""", sPath);
string sSourceConstr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=0;Extended Properties=""Excel 12.0 Xml;HDR=YES;""", sPath);
// string sSource = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties=Excel 8.0", sPath);
// string sDestConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
using (sSourceConnection)
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
string sql = "Select [Merchant_Name],[Store_Name],[Store_Address],[City] FROM [Sheet1$]";
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
conn.Open();
using (OleDbDataReader dr = command.ExecuteReader())
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
bulkCopy.DestinationTableName = "MerchantTempDetail";
bulkCopy.ColumnMappings.Add("Merchant_Name", "Merchant_Name");
bulkCopy.ColumnMappings.Add("Store_Name", "Store_Name");
bulkCopy.ColumnMappings.Add("Store_Address", "Store_Address");
bulkCopy.ColumnMappings.Add("City", "City");
bulkCopy.WriteToServer(dr);
【讨论】:
以上是关于OleDbDataReader 对象无法读取文件的主要内容,如果未能解决你的问题,请参考以下文章
OleDbDataReader 看不到单元格中的数据 (.xlsx)
如何在 VB.NET 中使用 OleDbDataReader 搜索数据?
获取 OleDbDataReader ASP.NET (VB) 返回的行数