从SQL Server表中删除一行

本文关键字:一行 删除 SQL Server | 更新日期: 2023-09-27 18:26:43

我正试图使用按钮事件从SQL Server数据库表中删除一整行。到目前为止,我的尝试都没有成功。这就是我要做的:

public static void deleteRow(string table, string columnName, string IDNumber)
{
    try
    {
    using (SqlConnection con = new SqlConnection(Global.connectionString))
    {
         con.Open();
         using (SqlCommand command = new SqlCommand("DELETE FROM " + table + " WHERE " + columnName + " = " + IDNumber, con))
         {
               command.ExecuteNonQuery();
         }
         con.Close();
    }
    }
    catch (SystemException ex)
       {
       MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
       }
    }
}

我一直收到错误:

System.Data.dll中首次出现"System.Data.SqlClient.SqlException"类型的异常出现错误:操作数类型冲突:文本与int 不兼容

表中的所有列都是TEXT类型。为什么我不能将类型为string的函数参数与列进行比较以找到匹配项?(然后删除行?)

从SQL Server表中删除一行

正如您所说,所有列名都是TEXT类型,因此,需要在IDNumber周围使用单引号将IDNumber用作TEXT。。。。。

    public static void deleteRow(string table, string columnName, string IDNumber)
    {
    try
    {
    using (SqlConnection con = new SqlConnection(Global.connectionString))
    {
         con.Open();
         using (SqlCommand command = new SqlCommand("DELETE FROM " + table + " WHERE " + columnName + " = '" + IDNumber+"'", con))
         {
               command.ExecuteNonQuery();
         }
         con.Close();
    }
    }
    catch (SystemException ex)
       {
       MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
       }
    }
 }

IDNumber应该是int而不是string,或者如果它真的是string,请添加引号。

更好的是,使用参数。

尝试使用参数

.....................
.....................
    using (SqlCommand command = new SqlCommand("DELETE FROM " + table + " WHERE " + columnName + " = " + @IDNumber, con))
             {
                   command.Paramter.Add("@IDNumber",IDNumber)
                   command.ExecuteNonQuery();
             }
.....................
.....................

使用语句时无需关闭连接

看起来IDNumber是一个字符串。它需要一个单独的报价。

"DELETE FROM " + table + " WHERE " + columnName + " = '" + IDNumber + "'"

您可以将"columnName"类型从TEXT更改为VARCHAR(MAX). TEXT。列不能与"="一起使用
请参阅本主题

private void button4_Click(object sender, EventArgs e)
{
    String st = "DELETE FROM supplier WHERE supplier_id =" + textBox1.Text;
    SqlCommand sqlcom = new SqlCommand(st, myConnection);
    try
    {
        sqlcom.ExecuteNonQuery();
        MessageBox.Show("delete successful");
    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void button6_Click(object sender, EventArgs e)
{
    String st = "SELECT * FROM suppliers";
    SqlCommand sqlcom = new SqlCommand(st, myConnection);
    try
    {
        sqlcom.ExecuteNonQuery();
        SqlDataReader reader = sqlcom.ExecuteReader();
        DataTable datatable = new DataTable();
        datatable.Load(reader);
        dataGridView1.DataSource = datatable;
    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

如果您正在使用MySql Wamp。这个代码有效。

string con="SERVER=localhost; user id=root; password=; database=dbname";
public void delete()
{
try
{
MySqlConnection connect = new MySqlConnection(con);
MySqlDataAdapter da = new MySqlDataAdapter();
connect.Open();
da.DeleteCommand = new MySqlCommand("DELETE FROM table WHERE ID='" + ID.Text + "'", connect);
da.DeleteCommand.ExecuteNonQuery();
MessageBox.Show("Successfully Deleted");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
private void DeleteProductButton_Click(object sender, EventArgs e)
{

    string ProductID = deleteProductButton.Text;
    if (string.IsNullOrEmpty(ProductID))
    {
        MessageBox.Show("Please enter valid ProductID");
        deleteProductButton.Focus();
    }
    try
    {
        string SelectDelete = "Delete from Products where ProductID=" + deleteProductButton.Text;
        SqlCommand command = new SqlCommand(SelectDelete, Conn);
        command.CommandType = CommandType.Text;
        command.CommandTimeout = 15;
        DialogResult comfirmDelete = MessageBox.Show("Are you sure you want to delete this record?");
        if (comfirmDelete == DialogResult.No)
        {
            return;
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message);
    }
}