C# Access Database Connection and Dml Operation (Select , Insert , Update , Delete )

        OleDbConnection con;
        OleDbDataAdapter da;
        OleDbCommand cmd;
        DataSet ds;
// SELECT OPERATION
        private void fillGrid()
        {
            con = new OleDbConnection(“Provider=Microsoft.ACE.Oledb.12.0;Data Source=market.accdb”);
            da = new OleDbDataAdapter(“Select *from Products”, con);
            ds = new DataSet();
            con.Open();
            da.Fill(ds, “product”);
            dataGridView1.DataSource = ds.Tables[“product”];
            con.Close();
        }
 // INSERT OPERATION
        private void InsertProduct()  
        {
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = “insert into products (ProductNo,ProductCode,ProductName) values (‘1′,’0250′,’Soap’)”;
            cmd.ExecuteNonQuery();
            con.Close();
        }
//UPDATE OPERATION
        private void UpdateProduct(int pProductNo)  
        {
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = “update products set ProductCode = 0275 Where  ProductNo=“+ pProductNo;
            cmd.ExecuteNonQuery();
            con.Close();
        }
 // DELETE OPERATION
        private void DeleteProduct(int pProductNo)  
        {
            cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = “delete from products where  ProductNo=“+ pProductNo;
            cmd.ExecuteNonQuery();
            con.Close();
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: