c# .net reading .mdb files

11,366

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); 

}
Share:
11,366
Toby Wilson
Author by

Toby Wilson

Software engineer in the automotive industry. Glider pilot & freelance games developer in my spare time.

Updated on June 26, 2022

Comments

  • Toby Wilson
    Toby Wilson almost 2 years

    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?