Reputation: 1507
It is necessary for me to write an application using C# .net that will edit specific .mdb files.
It's purely an in-department application for reading in-department files that debateably shouldn't be .mdb files, but I don't have a choice in that matter. I am pushing for changing to XML files, but that's a different matter.
The files are very much finite, 3ish tables with 100ish records each; so I hope to read the entire databases into application specific objects. Anything erroneous will be ignored and a new database copy will overwrite the previous one. The databases are a very specific format and easy for me to validate or throw out.
There seems to be many methods for actually reading/writing mdbs in .net which has left me confused. Can anyone suggest a best one?
Upvotes: 4
Views: 12210
Reputation: 29000
You can try with this code - based on Oledb provider
var mdb = "c:\MyDB.mdb";
var myDataTable = new DataTable();
using(var connection = new OleDbConnection(....."))
{
//Here sample format of string connection
//"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source="
//+ mdb + ";Mode=Share Deny None;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
connection.Open();
var query = "SELECT * from TABLE"; //Adjust your query
var adapter = new OleDbDataAdapter(query, connection); //This assigns the Select statement and connection of the data adapter
OleDbCommandBuilder oleDbCommandBuilder = new OleDbCommandBuilder(adapter); //This builds the update and Delete queries for the table in the above SQL. this only works if the select is a single table.
dadapt.Fill(MyDataTable);
}
Upvotes: 1
Reputation: 12521
I suggest you to use the Jet Engine's ODBC driver, which can be used with ADO.NET.
Upvotes: 1