簡體   English   中英

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);行上發生以下錯誤dataTable.Load(dataReader);

使用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)

使用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 )重現該問題。

我嘗試了以下方法來解決此問題。

我可以采取哪些其他步驟來解決問題或解決此問題?

對於第一個問題,請使用OleDbDataAdapterFill()方法或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連接字符串!

編輯:這是兩個驅動程序的鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM