SQL删除命令?

我在SQL中遇到一个带有意外结果的简单DELETE语句时出现问题,它似乎将该单词添加到列表中。 一定是傻事! 但我看不到它,尝试了几种不同的方式。 所有相同的结果都很混乱。

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

尝试删除单引号。 另外,为什么要将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; } ... 

另请注意,由于连接包含在using块中,因此您无需在finally语句中将其关闭。 Dispose方法将自动调用.Close方法,该方法将返回到ADO.NET连接池的连接,以便可以重用它。

另一个评论是,这个IncludeWord方法可以做很多事情。 它发送SQL查询来删除记录,它更新GUI上的一些文本框,它绑定一些列表=>这样的方法应该分开拆分,以便每个方法都有自己的特定职责。 否则,此代码只是维护方面的噩梦。 我强烈建议你编写只执行一个特定任务的方法,否则代码会很快变得完整。

 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 word=@word", conn); 

@Word不应该在sql查询中引用。

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

要调试它,请检查SqlCommand对象上的CommandText。 在进一步阅读之前,你应该试试这个。

问题在于在参数化的字符串周围添加单引号。 删除单引号,生活很美。 🙂

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

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

String queryForUpdateCustomer =“UPDATE customer SET cbalance = @ txtcustomerblnc WHERE cname =’”+ searchLookUpEdit1.Text +“’”; try {using(SqlCommand command = new SqlCommand(queryForUpdateCustomer,con)){

  command.Parameters.AddWithValue("@txtcustomerblnc", txtcustomerblnc.Text); con.Open(); int result = command.ExecuteNonQuery(); // Check Error if (result < 0) MessageBox.Show("Error"); MessageBox.Show("Record Update of Customer...!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); con.Close(); loader(); } } catch (Exception ex) { MessageBox.Show(ex.Message); con.Close(); }