【问题标题】:Delete a row from a SQL Server table从 SQL Server 表中删除一行
【发布时间】:2012-03-23 13:21:28
【问题描述】:

我正在尝试使用按钮事件从我的 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 类型的函数参数与列进行比较以找到匹配项? (然后删除该行?)

【问题讨论】:

  • 您应该为此使用参数。
  • 我不知道如何使用参数,有什么例子吗?资源?
  • 另外,作为旁注:首先,您不应该再使用TEXT - 从 SQL Server 2005 开始,它已被弃用。请改用VARCHAR(MAX)。另外:将所有列设为VARCHAR(MAX) 闻起来像个糟糕的设计——您真的需要每列 2 GB 的文本吗? REALLY!?!?!? 设计和性能提示:使用适当的数据类型

标签: c# sql-server delete-row


【解决方案1】:

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

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

【讨论】:

  • 对于此类操作,您应该始终使用参数化查询。
【解决方案2】:

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

更好的是,使用参数。

【讨论】:

  • 使用正则引号 (") 我得到这个:An error occurred: Invalid column name '50012'. 其中 50012 是 IDNumber(字符串)。
  • @ikathegreat - 试试 kashif 的例子。使用参数可以避免使用此类数据类型的麻烦。此外,正如其他发帖人所说,您几乎肯定不希望所有这些字段都是 TEXT。
【解决方案3】:

用参数试试

.....................
.....................

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

.....................
.....................

using语句无需关闭连接

【讨论】:

    【解决方案4】:

    看起来 IDNumber 是一个字符串。它需要用单引号括起来。

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

    【讨论】:

    • 返回:An error occurred: The data types text and varchar are incompatible in the equal to operator.
    【解决方案5】:

    您可以将“columnName”类型从TEXT 更改为VARCHAR(MAX). TEXT 列不能与"=" 一起使用。
    see this topic

    【讨论】:

      【解决方案6】:
      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);
          }
      }
      

      【讨论】:

        【解决方案7】:

        如果您使用的是 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);
        }
        }
        

        【讨论】:

          【解决方案8】:
          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);
          
              }
          }
          

          【讨论】:

          • 你的答案比这个 7 岁问题的公认答案有什么改进?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-16
          • 2023-03-04
          • 1970-01-01
          • 2010-09-05
          • 2020-09-30
          • 2020-07-07
          相关资源
          最近更新 更多