OleDbException (0x80004005): 未知
OleDbException (0x80004005): Unknown
我有以下代码(Windows 表单的一部分)在我的机器上成功连接到 Excel 文件,但在不同的机器上却失败了。
var fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
var extendedProperties = string.Empty;
if (fd.FileName.Contains(".xlsx"))
{
// excel 2007 xml format file, IMEX = import data as text avoids data conversion errors
extendedProperties = "Excel 12.0 Xml;IMEX=1";
}
else if (fd.FileName.Contains(".xls"))
{
// excel 2003 format file, IMEX: import data as text avoids data conversion errors
extendedProperties = "Excel 8.0;IMEX=1";
}
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fd.FileName + "';Extended Properties='" + extendedProperties + "'";
using (var objXConn = new OleDbConnection(connectionString))
{
objXConn.Open();
var selectStatement = string.Format("SELECT * FROM [tabName$] WHERE FormType IS NOT NULL");
var dataTable = new DataTable("test");
using (var objCommand = new OleDbCommand(selectStatement, objXConn))
{
var dataReader = objCommand.ExecuteReader();
if (dataReader != null)
{
dataTable.Load(dataReader);
}
}
using (var stringWriter = new StringWriter())
{
dataTable.WriteXml(stringWriter);
this.textBox1.Text = stringWriter.ToString();
}
}
}
使用 OleDbDataAdapter
而不是 OleDbCommand.ExecuteReader()
可以重现该行为。
using (var dataAdapter = new OleDbDataAdapter(selectStatement, objXConn))
{
dataAdapter.Fill(dataTable);
}
失败,我的意思是在 dataTable.Load(dataReader);
行发生以下错误
当running/building x86 配置的程序时,出现以下错误。
System.Data.OleDb.OleDbException (0x80004005): Unknown
at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbDataReader.GetRowHandles()
at System.Data.OleDb.OleDbDataReader.ReadRowset()
at System.Data.OleDb.OleDbDataReader.Read()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
at ExcelTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
当running/building配置为AnyCPU/x64的程序时,我在两台机器上都得到以下错误。
System.InvalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
at System.Data.OleDb.OleDbServicesWrapper.GetDataSource(OleDbConnectionString constr, DataSourceWrapper& datasrcWrapper)
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.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.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at ExcelTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
用于测试的 Excel 文件在两台机器上都是同一个文件。我已经能够使用多个文件重现该问题(.xls
和 .xlsx
)。
我已经尝试了以下方法来解决这个问题。
- 正在安装'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
中描述的组件
- 正在将项目配置为 x86/x64
- 卸载并重新安装2007 Office System Driver: Data Connectivity Components
- 卸载并重新安装Microsoft Access Database Engine 2010 Redistributable
我可以采取哪些额外步骤来解决或解决此问题?
第一个问题使用OleDbDataAdapter
和Fill()
方法或Update()
。
DataTable newTbl = new DataTable()
using(OleDbDataAdapter ad = new OleDbDataAdapter(
@"SELECT * FROM [tabName$] WHERE FormType IS NOT NULL", objXConn))
{
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
// Saves the data set
ad.Update(newTbl);
}
32 位和 64 位应用程序的 OLEDB 驱动程序不同。
如果您只安装了 32 位驱动程序,那么 64 位应用程序尝试使用它会出现此错误:Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
。同样,如果您只安装了 64 位版本,而 32 位应用程序尝试使用它,则会出现此错误。
要了解发生了什么,您应该检查您的应用程序是什么。我认为这条线路会有所帮助:Environment.Is64BitProcess
如果 xls(Excel 2003) 文件出现问题,请尝试使用 JET 连接字符串!
编辑:
这是 drivers 的 link:
我有以下代码(Windows 表单的一部分)在我的机器上成功连接到 Excel 文件,但在不同的机器上却失败了。
var fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
var extendedProperties = string.Empty;
if (fd.FileName.Contains(".xlsx"))
{
// excel 2007 xml format file, IMEX = import data as text avoids data conversion errors
extendedProperties = "Excel 12.0 Xml;IMEX=1";
}
else if (fd.FileName.Contains(".xls"))
{
// excel 2003 format file, IMEX: import data as text avoids data conversion errors
extendedProperties = "Excel 8.0;IMEX=1";
}
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fd.FileName + "';Extended Properties='" + extendedProperties + "'";
using (var objXConn = new OleDbConnection(connectionString))
{
objXConn.Open();
var selectStatement = string.Format("SELECT * FROM [tabName$] WHERE FormType IS NOT NULL");
var dataTable = new DataTable("test");
using (var objCommand = new OleDbCommand(selectStatement, objXConn))
{
var dataReader = objCommand.ExecuteReader();
if (dataReader != null)
{
dataTable.Load(dataReader);
}
}
using (var stringWriter = new StringWriter())
{
dataTable.WriteXml(stringWriter);
this.textBox1.Text = stringWriter.ToString();
}
}
}
使用 OleDbDataAdapter
而不是 OleDbCommand.ExecuteReader()
可以重现该行为。
using (var dataAdapter = new OleDbDataAdapter(selectStatement, objXConn))
{
dataAdapter.Fill(dataTable);
}
失败,我的意思是在 dataTable.Load(dataReader);
当running/building x86 配置的程序时,出现以下错误。
System.Data.OleDb.OleDbException (0x80004005): Unknown
at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbDataReader.GetRowHandles()
at System.Data.OleDb.OleDbDataReader.ReadRowset()
at System.Data.OleDb.OleDbDataReader.Read()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
at ExcelTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
当running/building配置为AnyCPU/x64的程序时,我在两台机器上都得到以下错误。
System.InvalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
at System.Data.OleDb.OleDbServicesWrapper.GetDataSource(OleDbConnectionString constr, DataSourceWrapper& datasrcWrapper)
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.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.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at ExcelTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
用于测试的 Excel 文件在两台机器上都是同一个文件。我已经能够使用多个文件重现该问题(.xls
和 .xlsx
)。
我已经尝试了以下方法来解决这个问题。
- 正在安装'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine 中描述的组件
- 正在将项目配置为 x86/x64
- 卸载并重新安装2007 Office System Driver: Data Connectivity Components
- 卸载并重新安装Microsoft Access Database Engine 2010 Redistributable
我可以采取哪些额外步骤来解决或解决此问题?
第一个问题使用OleDbDataAdapter
和Fill()
方法或Update()
。
DataTable newTbl = new DataTable()
using(OleDbDataAdapter ad = new OleDbDataAdapter(
@"SELECT * FROM [tabName$] WHERE FormType IS NOT NULL", objXConn))
{
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
// Saves the data set
ad.Update(newTbl);
}
32 位和 64 位应用程序的 OLEDB 驱动程序不同。
如果您只安装了 32 位驱动程序,那么 64 位应用程序尝试使用它会出现此错误:Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
。同样,如果您只安装了 64 位版本,而 32 位应用程序尝试使用它,则会出现此错误。
要了解发生了什么,您应该检查您的应用程序是什么。我认为这条线路会有所帮助:Environment.Is64BitProcess
如果 xls(Excel 2003) 文件出现问题,请尝试使用 JET 连接字符串!
编辑: 这是 drivers 的 link: