Showing posts with label Adding Column. Show all posts
Showing posts with label Adding Column. Show all posts

Friday, April 27, 2018

How to Add Column in Microsoft Access Database File using C#

In adding column in Microsoft Access database file using C#, the code snippet below might help you.


using System.Data.OleDb;

string fileName = "YourDatabaseFilename";
string password = "TheDatabasePassword";

string tableName = "TableName";
string columnName = "NewColumn";
string dataType = "Number";    
// it could be "LongText", "ShortText", kindly refer to the data types accepted by MS Access
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 = "ALTER TABLE NewTable ADD COLUMN " + columnName + " " + dataType;
OleDbCommand Command = new OleDbCommand(query, con);
Command.ExecuteNonQuery();
con.Close();