Friday, April 27, 2018

How to Insert Data into Microsoft Access Database Using C#

To insert data to the Microsoft Access database, the code snippet below will do the job.

using System.Data.OleDb;

string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";
string tableName = "TableName";
string columnName = "NewColumn";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + fileName +
                                            ".accdb; Jet OLEDB:Database Password =" + password + "; Mode=12";

OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query = 
"INSERT INTO " + tableName + "(" + columnName +
                         ") VALUES('" + info + "')";
OleDbCommand Command = new OleDbCommand(query, con);
Command.ExecuteNonQuery();
con.Close();

No comments:

Post a Comment