如果下一行相同,如何只删除1行

时间:2018-06-25 13:56:33

标签: c# visual-studio-2015

private void button3_Click(object sender, EventArgs e)
{               
    con.Open();
    SqlDataAdapter SAD = new SqlDataAdapter("DELETE FROM Table1 WHERE ID_='"+textBox4.Text.ToString() + "'", con);
    SAD.SelectCommand.ExecuteNonQuery();
    MessageBox.Show("DELETE ALREADY!");
    string query = "SELECT * FROM Table1";
    SqlDataAdapter SDA = new SqlDataAdapter(query, con);
    DataTable dt = new DataTable();
    SDA.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();                     
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您想删除重复项,但只保留一个。 你可以试试这个吗?

DELETE FROM Table1
    WHERE ID_ NOT IN
    (
    SELECT ID_
    FROM Table1
    )