UPDATE, DELETE in c#

本文关键字:in DELETE UPDATE | 更新日期: 2023-09-27 18:25:18

有人能告诉我如何用sql compact edition 4.0在c#中执行UPDATE、DELETE sql命令吗??我在另一篇文章中找到了这个代码。我可以在这里执行UPDATE/DELETE吗??plz帮助。。

string strConn = Properties.Settings.Default.SqlConnectionString;
using (SqlCeConnection con = new SqlCeConnection(strConn))
{
    con.Open();
    using (SqlCeCommand cmd = new SqlCeCommand("insert into CustTable(ID, Name) values (@Val1, @val2)", con))
    {
        cmd.Parameters.AddWithValue("@Val1", customer.ID);
        cmd.Parameters.AddWithValue("@Val2", customer.Name);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.ExecuteNonQuery();
    }
}

UPDATE, DELETE in c#

我举了一个例子,但我真的建议你在进行任何项目之前读一些好书

更新命令

using (SqlCeConnection con = new SqlCeConnection(strConn))
{
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("update CustTable set col1 = @Val1", con))
{
    cmd.Parameters.AddWithValue("@Val1", customer.ID);
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.ExecuteNonQuery();
}
}

删除命令

using (SqlCeConnection con = new SqlCeConnection(strConn))
{
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("DELETE from CustTable where col1 = @Val1", con))
{
    cmd.Parameters.AddWithValue("@Val1", customer.ID);
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.ExecuteNonQuery();
}
}