I recently asked a question about how to insert 100,000 records in an MDB file in C#. The given answers reduced the required time from 45 secs to 10 secs. It was even possible to be reduced to 2~3 secs using Number Tables.
Now I have problem updating a similar database. I don't want to actually update 100,000 records in this case but around 10,000 records from the already created MDB file with 100,1000 records.
Here is my code:
Stopwatch sw = new Stopwatch();
sw.Start();
OleDbConnection con = new OleDbConnection();
string dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;";
string dbSource = "Data Source = D:/programming/sample.mdb";
con.ConnectionString = dbProvider + dbSource;
con.Open();
string query = "SELECT * FROM tblBooks";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(query, con);
da.Fill(ds,"Books Table");
for (int i = 0; i < 10000; i++)
{
ds.Tables[0].Rows[i][1] = "Book" + i.ToString();
}
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
da.Update(ds, "Books Table");
con.Close();
sw.Stop();
Console.WriteLine(String.Format("{0:0.0} seconds", sw.ElapsedMilliseconds / 1000.0));
Updating 10000 records (only one field) took around 24 secs!
I have another code that performs well:
Stopwatch sw = new Stopwatch();
sw.Start();
OleDbConnection con = new OleDbConnection();
string dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;";
string dbSource = "Data Source = D:/programming/sample.mdb";
con.ConnectionString = dbProvider + dbSource;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "UPDATE tblBooks SET [Title] = @title";
cmd.Parameters.AddWithValue("@title", "Book");
cmd.ExecuteNonQuery();
con.Close();
sw.Stop();
Console.WriteLine(String.Format("{0:0.0} seconds", sw.ElapsedMilliseconds / 1000.0));
I found out that when I use the above code I'm able to update the whole table (100,000 records) in less than a second (0.4 sec). But in this version, I don't know how to be selective and only update part of the table and how to assign different values to each record (Book 1, Book 2...). I mean I want to be able to update for example from record 4000 to 14000 in the table and assign Book 1, Book 2 and ... to title field.