Update a Microsoft Access Database
Despite the fact that there is a free version of SQL Server called SQL Server Express, there are still applications that require using a Microsoft Access database. But since these are dwindling in number, there are few articles or posts devoted to accessing Access.
This post (and my prior post that demonstrates how to retrieve Access data) attempt to rectify that issue.
The code is first shown in both VB and C#. It is then described in detail below.
NOTE: Be sure to set a reference to System.Data.OleDb.
string update = "Update Customer Set Title = ? Where PersonId = ?";
string cnnString =
using (var cnn = new OleDbConnection(cnnString))
using (var cmd = new OleDbCommand(update, cnn))
// Add the parameters
// Execute the command
Dim update As String = _
"Update Customer Set Title = ? Where PersonId = ?"
Dim cnnString As String = _
Using cnn As New OleDbConnection(cnnString)
Using cmd As New OleDbCommand(update, cnn)
‘ Add the parameters
‘ Execute the command
The code begins by declaring several variables. The first variable is the Update statement. Change it to update the desired fields from the desired table. Use "?" as placeholders for command parameters.
The connection string uses the Jet OleDb data provider. Change the connection string Data Source property to the directory and name of your Access database file. If you provide no directory (like in this example), it will assume that the file is located in the same directory as the executing application.
The first using statement defines the connection. The connection is then opened. The connection is automatically closed at the end of the using block.
The second using statement creates the command using the defined query and opened connection. Use the AddWithValue method of the command parameter’s collection to add the parameter names and values. There should be an AddWithValue method call for each "?" placeholder in the Update statement.
Finally, execute the command using the ExecuteNonQuery method.