How to use ms access database in C#.Net

How to use ms access database in C#.Net. I am just a beginner. I don't have any idea of connecting ms access database with C#. Any Examples.. Thanks in advance.
Posted by Harpreet 3/22/2014 10:58:16 PM
Total Answers : 1
Answer 1
Step 1.
Use below namespace.

using System.Data.OleDb;

Step 2.
Write below code on click event of the button.

try
{
OleDbConnection con = new OleDbConnection("YourConnectionString");
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into table1(name,age,class)
values('Harish Sharma','22','B.A Part 1')", con);
cmd.ExecuteNonQuery();
lbl_msg.Text = "Record inserted successfully.";
con.Close();
}
catch
{
lbl_msg.Text ="Error Occured.";
}

Below is the explanation.
Line 1.
Create connection with your database. Replace "YourConnectionString" with your own connection string.

Line 2.
Open Your Connection.

Line 3.
Create Sql command for inserting records. Here i am inserting the records into columns "name","age","class"of table "table1". Also pass your connection in oledbcommand.

Line 4.
Execute Query.

Line 5.
Show Message if record inserted successfully.

Line 6.
Close the connection.
Posted by StepCoder Team 3/24/2014 5:24:29 PM
Post your answer
Preview
Sign in to your account
Your Email : 
Password :   
Forgot your password
Don't have StepCoder account ?
Create a new account
 
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
Answer these questions! If you know the answer.