Pages

Thursday, 17 October 2013

How to store data in ms access using c#.net

Step 1
Start the visual studio 2010 and design the form like

Step 2
Create the database like

Step 3
Connect the access database form server explorer like
Server explorer->add connection->

Browse ->select the particular access database

Check test connection and press ok.
Step 4
Write the coding like
using System.Data.OleDb;
namespace example
{
    public partial class Form1 : Form
    {
        OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ANNAS\Documents\Visual Studio 2010\Projects\example\exam.mdb");

//get the connection link from server explorer choose particular database properties for connection string


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show(" fill all the fields ");
            }
            else
            {
                string vsql = string.Format("insert into new values('{0}',{1})", textBox1.Text, int.Parse(textBox2.Text));
                OleDbCommand vcom = new OleDbCommand(vsql, vcon);
                vcom.ExecuteNonQuery();
                vcom.Dispose();
                MessageBox.Show(" data entered successfully ");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            vcon.Open ();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
    }
}

Step 5
Execute the project like

Step 6
Now check the database like
You have now

Step 7
Execute success fully
Than you
By R.Boobalan