在c#中删除创建表单中的记录

时间:2011-05-17 05:51:07

标签: c#

我正在使用c#,在此我想从表单中删除记录,因为我有以下代码..

     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)";

    }

但我没有得到答案......

2 个答案:

答案 0 :(得分:2)

您需要设置OleDbCommand

您案例的完整代码,

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();

    }

有关整个实施的更多信息,请阅读此处 http://www.java2s.com/Code/CSharp/Database-ADO.net/DeletedatabaserecordsthroughOleDbCommandandverifytheresults.htm

答案 1 :(得分:0)

您需要使用SQL和连接执行OleCommand