Rockstar
Rockstar

Reputation: 25

Deleting records from the created form in c#

I m working with c#, in this i want to delete record from the form for that i have the following code..

     private void button3_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data  Source=C:\\Documents and Settings\\mayur patil\\My Documents\\Dairy_db\\tblCompany.mdb";
        con.Open();
        string sql = "DELETE FROM tblCompany WHERE (CoCode = 1)";

    }

but i m not getting the answer...

Upvotes: 0

Views: 56

Answers (2)

Oded
Oded

Reputation: 499132

You need to execute an OleCommand with your SQL and connection.

Upvotes: 0

drhanlau
drhanlau

Reputation: 2527

You need to setup an OleDbCommand

The full code for your case,

private void button3_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data  Source=C:\\Documents and Settings\\mayur patil\\My Documents\\Dairy_db\\tblCompany.mdb";
        string sql = "DELETE FROM tblCompany WHERE (CoCode = 1)";

        con.Open();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

    }

For more information of the whole implementation, read here http://www.java2s.com/Code/CSharp/Database-ADO.net/DeletedatabaserecordsthroughOleDbCommandandverifytheresults.htm

Upvotes: 2

Related Questions