I'm trying to connect to a mdb file and I understand that I would need Microsoft.OLEDB.JET.4.0
data provider. Unfortunately, I do not have it installed on the (University) machine.
Since, they don't provide that provider, I believe there should be a way around.
How can I connect to the file without Microsoft.OLEDB.JET.4.0
or is there any alternative ?
I have following providers:
I have tried using OLE DB Provider for Microsoft Directory Services
, to which while testing connection, I get 'Test succeeded but some settings were not accepted by the provider'. I took that string and used it anyway and I got ADsDSOObject' failed with no error message available, result code: DB_E_ERRORSINCOMMAND(0x80040E14)
.
The simplest way to connect is through an OdbcConnection using code like this
using System.Data.Odbc;
using(OdbcConnection myConnection = new OdbcConnection())
{
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
//execute queries, etc
}
where myConnectionString is something like this
myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};" +
"Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;
See ConnectionStrings
In alternative you could create a DSN and then use that DSN in your connection string
now your connectionString could be written in this way
myConnectionString = "DSN=myDSN;"
Here's how to use a Jet OLEDB or Ace OLEDB Access DB:
using System.Data;
using System.Data.OleDb;
string myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\myPath\myFile.mdb;" +
"Persist Security Info=True;" +
"Jet OLEDB:Database Password=myPassword;";
try
{
// Open OleDb Connection
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
// Execute Queries
OleDbCommand cmd = myConnection.CreateCommand();
cmd.CommandText = "SELECT * FROM `myTable`";
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); // close conn after complete
// Load the result into a DataTable
DataTable myDataTable = new DataTable();
myDataTable.Load(reader);
}
catch (Exception ex)
{
Console.WriteLine("OLEDB Connection FAILED: " + ex.Message);
}
You should use "Microsoft OLE DB Provider for ODBC Drivers" to get to access to Microsoft Access. Here is the sample tutorial on using it
http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With