OleDbConnection gets “External component has thrown an exception.”

主宰稳场 提交于 2020-02-15 07:01:29

问题


I' m using a Windows Forms application to export data to excel.

Application is built both x64 and x86.

So both version of Microsoft Access Databse Engine must be installed to work the application on same computer.

Firstly AccessDatabaseEngine.exe is installed and AccessDatabaseEngine_x64.exe is installed passive.

x64 version of application is working

But x86 version is getting

SEHException: "External component has thrown an exception."

 at System.Data.Common.UnsafeNativeMethods.IDBInitializeInitialize.Invoke(IntPtr pThis)
 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, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
 at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
 at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
 at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
 at System.Data.OleDb.OleDbConnection.Open()

The code part is below;

var accessConnection = new OleDbConnection(connectionString);
accessConnection.Open();

And ConnectionString is

Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\Me\Desktop\ExportTest.xls";Extended Properties="Excel 8.0;HDR=Yes";

How can I solve it?


回答1:


This will usually occur when the build configuration platform in Visual Studio is incorrect, this can occur in both build configuration platforms, x86 and x64.

This is due to a mismatch between the build configuration platform of your project and the Microsoft Access Database Engine which is installed on your machine.

In order to resolve this error:

  • Change the build configuration platform in Visual Studio
  • make sure it matches
  • the Microsoft Access Database Engine version on your machine
  • Recompile and run your project
  • The run time error should now be resolved



回答2:


I can propose a horrible solution but when you are under pressure, it works. Open the Excel file and save it as an xls file instead of xlsx. Install the ACEOLEDB.12 32 bit version and pull your data in that way. This is not the answer, but it may suffice as a work-around.

For most occasions, the "Flat File" import in SQL Server option will work just fine.



来源:https://stackoverflow.com/questions/35701585/oledbconnection-gets-external-component-has-thrown-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!