How to create table in MS-Access database using C# code?
Microsoft Access database is a database management system
from Microsoft that combines the relational Microsoft Jet Database Engine with
a graphical user interface and software-development tools. In this blog I will
explain you how to create a table in MS-Access database. Let’s take a look on
it.
To create a table in MS-Access database, first of all you’ve
an existence database file or you have to create a new database file. After
creating the database file; just write the following line of code to create the
table in MS-Access database.
Code for MS-Access 2003:
//
Creating OLEDB connection string for Ms-Access 2003 database file
OleDbConnection myConnection
= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source= D:\\Arun Singh\\MsAccessFile\\MyDatabase1.mdb; OLE DB
Services=-1");
// Open
the connection
myConnection.Open();
// Create
Oledb command to execute particular query
OleDbCommand myCommand
= new OleDbCommand();
myCommand.Connection
= myConnection;
// Query
to create table with specified data columne
myCommand.CommandText
= "CREATE TABLE
tblIdentityTesting([MyIdentityColumn] long, [Name] text)";
myCommand.ExecuteNonQuery();
MessageBox.Show("Table Created Successfully");
Code for MS-Access 2007:
//
Creating OLEDB connection string for Ms-Access 2007 database file
OleDbConnection myConnection
= new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= D:\\Arun Singh\\MsAccessFile\\MyDatabase.accdb;Persist Security
Info=False;");
// Open
the connection
myConnection.Open();
// Create
Oledb command to execute particular query
OleDbCommand myCommand
= new OleDbCommand();
myCommand.Connection
= myConnection;
// Query
to create table with specified data columne
myCommand.CommandText
= "CREATE TABLE
tblIdentityTesting([MyIdentityColumn] long, [Name] text)";
myCommand.ExecuteNonQuery();
MessageBox.Show("Table Created Successfully");
This is a brief description about on creating a table in
MS-Access database using C# code. I hope this blog will help you in
understanding that how to create a table in MS-Access database using C# code.