I am trying to connect to a Access database. I have setup in advance a Access db called "ToAccess.mdb". It has 4 columns: id, col1, col2 and col3. I want to populate it with the partial program below.
In Visual Studio 2013 I have setup in web.config this excerpt:
<connectionStrings><add name="Accessdb" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\a Access\ToAccess.mdb;Persist Security Info=False;" /> </connectionStrings>
In Server Explorer>Data Connections>Add Connection... I selected Data Source:
Microsoft Access Database File (OLE DB)
I went to my Access file: "C:\Users\User\Documents\a Access\ToAccess.mdb"
Here is my code:
String connStr1 = ConfigurationManager.ConnectionStrings["Accessdb"].ConnectionString; String cmdStr1 = "INSERT INTO [TableAccess] (col1,col2,col3) VALUES (@col1,@col2,@col3);"; try { using (OleDbConnection conn1 = new OleDbConnection(connStr1)) { using (OleDbCommand cmd1 = new OleDbCommand(cmdStr1, conn1)) { conn1.Open(); cmd1.Parameters.AddWithValue("@col1", dr[1]); cmd1.Parameters.AddWithValue("@col2", dr[2]); cmd1.Parameters.AddWithValue("@col3", dr[3]); cmd1.ExecuteNonQuery(); conn1.Close(); cmd1.Dispose(); conn1.Dispose(); } } } catch (Exception ex) { Label2.Text = "Insert Into: " + ex.ToString(); }
Error code:
Insert Into: 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.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) 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.DbConnectionClosed.TryOpenConnection(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 MDFtoAccess._Default.Button1_Click(Object sender, EventArgs e) in c:\Users\John\Documents\a ASP.net\csharp\a MDF and Microsoft Office\MDFtoAccess\MDFtoAccess\Default.aspx.cs:line 43