Inserting C# windows form data into Ms Access

    Program in C# for creating  a connection with Ms access      and storing data in Access.

1. Create a Windows form like this.




2. Now go on new and create a new database in Ms Access.

3. After creating database create a new table in it.

Here we have create a database with name MCA4th and table with student name.

4. Now for creating a connection with database you need to add  using System.Data.OleDb; in form.

5.Now you need to create a connection object.
Code is this:

  public partial class Form1 : Form
    {private OleDbConnection connection= new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
        }

6. Now write this code on button.

private void button1_Click(object sender, EventArgs e)
        {
            
            connection.ConnectionString=@"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Documents and Settings\admin\My Documents\mca4th.accdb";
        connection.Open();
        OleDbCommand command= new OleDbCommand();
        command.Connection=connection;
        command.CommandText = "Insert into student (Name,Age,Class)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "') ";
    command.ExecuteNonQuery();
    MessageBox.Show("Submitted");
    }


OUTPUT:-




Comments

Popular posts from this blog

Java Project Code: Cinema Ticket booking system : Create a Java Application to book a ticket in a Cinema hall.