Introduction
I've searched all over the internet for a simple tutorial on how to connect to a database, and also modify it. All I found were tutorials that were far too complex, and just had way to much code. Here I offer a tutorial with only the code needed to connect and manipulate a database.
Using the Code
There is no exception handling within the code. That part will be left up to the ones using the tutorial for their own benefit. I just offer descriptions on how to complete certain processes.
private static string accessConn =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\HPC\Documents\MyDatabase.mdb";
private static string accessSelect = "SELECT * FROM Table1";
private static OleDbConnection conn;
private static OleDbCommand comm;
private static OleDbDataAdapter adapter;
private static DataSet ds;
private static DataTable dt;
conn = new OleDbConnection(accessConn);
conn.Open();
comm = new OleDbCommand(accessSelect, conn);
adapter = new OleDbDataAdapter(accessSelect, conn);
new OleDbCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds, "Table1");
DataTable dt = ds.Tables["Table1"];
DataRow dr = dt.NewRow();
dr["Column1"] = "TestInsertMethod";
dt.Rows.Add(dr);
adapter.Update(dt);
conn.Close();
Points of Interest
When creating the Command Object, make sure to add all other command types as well if you wish to do more than to add to the datatable.
History
- 07/03/2010 - Initial version