2011-06-20 107 views
2

我在SQL中遇到了一个简单的DELETE语句出现意外结果的麻烦,它似乎将这个单词添加到列表中。必须是愚蠢的东西!但我看不到它,尝试了几种不同的方法。所有相同的结果如此混乱。SQL删除命令?

public void IncludeWord(string word) 
{ 
    // Add selected word to exclude list 
    SqlConnection conn = new SqlConnection(); 
    String ConnectionString = "Data Source = dev\\SQLEXPRESS ;" + "Initial Catalog=sml;" + "User id=** ;" + "Password =*;" + "Trusted_Connection=No"; 

    using (SqlConnection sc = new SqlConnection(ConnectionString)) 
    { 
     try 
     { 
      sc.Open(); 

      SqlCommand Command = new SqlCommand(
       "DELETE FROM excludes WHERE word='@word'" + 
       conn); 


      Command.Parameters.AddWithValue("@word", word); 
      Command.ExecuteNonQuery(); 
     } 
     catch (Exception e) 
     { 
      Box.Text = "SQL error" + e; 
     } 
     finally 
     { 
      sc.Close(); 
     } 
     ExcludeTxtbox.Text = ""; 

     Box.Text = " Word : " + word + " has been removed from the Exclude List"; 

     ExcludeLstBox.AppendDataBoundItems = false; 
     ExcludeLstBox.DataBind(); 
    } 
+0

该代码不会添加条目而不是删除它。你的问题在别处。请张贴与此方法交汇的任何相关位。 –

回答

11

尝试删除单引号。你为什么要连接你的SQL字符串和连接对象(.. word='@word'" + conn)?

尝试这样的:

try 
{ 
    using (var sc = new SqlConnection(ConnectionString)) 
    using (var cmd = sc.CreateCommand()) 
    { 
     sc.Open(); 
     cmd.CommandText = "DELETE FROM excludes WHERE word = @word"; 
     cmd.Parameters.AddWithValue("@word", word); 
     cmd.ExecuteNonQuery(); 
    } 
} 
catch (Exception e) 
{ 
    Box.Text = "SQL error" + e; 
} 
... 

还要注意的是,因为连接被包裹在一个使用块,你并不需要关闭它在finally声明。 Dispose方法将自动调用.Close方法,该方法将返回到ADO.NET连接池的连接,以便可以重用它。

另一种说法是,这种方法做了很多事情。它发送SQL查询来删除记录,它更新GUI上的一些文本框,并绑定一些列表=>这样的方法应该分开分割,以便每种方法都有其特定的责任。否则这个代码在维护方面只是一场噩梦。我强烈建议你编写只执行一个特定任务的方法,否则代码很快就会变成一团糟。

+0

谢谢我回到所有的代码,发现它从我不想调用的函数分支出来。我重写了删除功能以及它的所有工作!感谢提示。 – user685590

1

@Word不应在sql查询中引号。

不知道你为什么试图在sql查询的末尾添加连接。

2
SqlCommand Command = new SqlCommand(
        "DELETE FROM excludes WHERE word='@word'" + 
        conn); 

SqlCommand Command = new SqlCommand(
        "DELETE FROM excludes WHERE word='@word'", 
        conn); 

还通过删除单引号代替别人的建议是这样

SqlCommand Command = new SqlCommand(
        "DELETE FROM excludes WHERE [email protected]", 
        conn); 
0

要调试这一点,研究在CommandText SqlCommand对象上。在进一步阅读之前,您应该尝试一下。

问题出在绕参数化的字符串添加单引号。删除单引号,生活是美丽的。 :-)

哦,你的conn是一个对象,需要一个逗号,而不是+。

0

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 supplier"; 

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