C# 如何检查副本

C# 如何检查副本,c#,C#,此代码将Id、名称等存储到数据库中。但是当有相同的Id时,我想删除它。当我删除它时,相同Id的两个都将被删除,我不希望这样,所以在将其存储到数据库之前,是否仍要检查副本 如果可能的话,我想这样做: private void Add_Box_Click(object sender, EventArgs e) { string phoneNumber; if (string.IsNullOrWhiteSpace(Id_Box.Text))// To check if the Id_b

此代码将Id、名称等存储到数据库中。但是当有相同的Id时,我想删除它。当我删除它时,相同Id的两个都将被删除,我不希望这样,所以在将其存储到数据库之前,是否仍要检查副本

如果可能的话,我想这样做:

private void Add_Box_Click(object sender, EventArgs e)
{
    string phoneNumber;
    if (string.IsNullOrWhiteSpace(Id_Box.Text))// To check if the Id_box is empty or not
    {
        MessageBox.Show("Please Enter Your ID");// need to enter ID in order to save data
    }
    ///////////////////////////////////////////check the Extension Box////////////////////////////////////////////////////////////////////////////////////
    else
    {
        if (string.IsNullOrWhiteSpace(Ext_Box.Text))
        {
            phoneNumber = Phone_Box.Text;// if it is empty then it will only show the phone number
        }
        else
        {

            phoneNumber = Phone_Box.Text + "," + Ext_Box.Text; // show the phone number and the extension if there is something in the extension
        }
        ///////////////////////////////////////////////////////////Save it to the Database///////////////////////////////////////////////////////
        SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Contact_List(Id, Name, Adress1, Adress2, City, Province, Postal_Code, Phone, Email)VALUES('" + Id_Box.Text + "','" + Name_Box.Text + "','" + Adress1_Box.Text + "','" + Adress2_Box.Text + "','" + City_Box.Text + "','" + Province_Box.Text + "','" + Code_Box.Text + "','" + phoneNumber + "','" + Email_Box.Text + "')", con);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Information Added", "Confirm");
        /////////////////////////////////////Show new set of data after insert a new data/////////////////////////////////////////////////////////////              
        SqlCeCommand cmd2 = new SqlCeCommand("Select * from Contact_List;", con);
        try
        {
            SqlCeDataAdapter sda = new SqlCeDataAdapter();
            sda.SelectCommand = cmd2;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            BindingSource bs = new BindingSource();
            bs.DataSource = dt;
            dataGridView1.DataSource = bs;
            sda.Update(dt);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        ////////////////////////////////Empty The Box/////////////////////////////////////////////////////////////////////////////////////////////////
        Id_Box.Text = String.Empty;
        Name_Box.Text = String.Empty;
        Adress1_Box.Text = String.Empty;
        Adress2_Box.Text = String.Empty;
        City_Box.Text = String.Empty;
        Province_Box.Text = String.Empty;
        Code_Box.Text = String.Empty;
        Phone_Box.Text = String.Empty;
        Ext_Box.Text = String.Empty;
        Email_Box.Text = String.Empty;
    }

}

可能吗?

在执行
INSERT
SQL语句之前,请尝试运行SQL
int ContactCount=(int)cmd.ExecuteScalar(“从联系人列表中选择COUNT(*),其中Id=”“+Id\u Box.Text+”)

如果
ContactCount>0
,则您可以执行删除建议

我还建议您使用SQL更新,而不是删除和插入相同的记录


另外,请阅读SQL注入攻击。像您在这里所做的那样,使用用户输入的值构建SQL语句会使您暴露于这种类型的漏洞。

首先,与所有这些答案一样:不要使用字符串连接,而是使用参数化查询来防止SQL注入

对于您的问题:

你可以选择

if ( the values in id column == to the Id_textBox) {
    MessageBox.Show("Duplicate ,PLease enter anotherId")
}
如果
count>0
则id已存在

或者你可以做一个

string query = "SELECT count(*) from ContactList Where id = @id";
SqlCeCommand cmd = new SqlCeCommand(query, connection);
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 50).Value = Id_Box.Text;
int count = (int)cmd.ExecuteScalar();

count
随后将包含受影响的行数,即如果该值已存在,则为
0
;如果该值不存在,但新插入,则为
1

而不是通过将所有框设置为string.Empty来清空所有框,为什么不使用foreach循环或linq检查类型是否为TextBox,然后将TextBox.Text设置为string.Empty,例如
yourForm.Controls.OfType().ToList().foreach(TextBox=>TextBox.Clear())还有,为什么不检查一下id,例如从id=@id参数的表中选择Count(*)。。如果计数>1,则在允许插入之前显示副本…几天前您基本上问了相同的问题,代码更少。您是否考虑使用谷歌而不是StAdvExcel?你的答案在互联网上有几千个版本。自己找到答案通常比等上几天有人给你做作业要快
string query "IF NOT EXISTS(SELECT count(*) from ContactList Where id = @id) INSERT INTO ContactList(Id, ...) VALUES(@id, ...)";
SqlCeCommand cmd = new SqlCeCommand(query, connection);
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 50).Value = Id_Box.Text;
int count = cmd.ExecuteNonQuery();