我在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();
}
答案 0 :(得分:16)
尝试删除单引号。另外,为什么要将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上的一些文本框,并绑定一些列表=>这样的方法应该分开拆分,以便每个方法都有自己的特定职责。否则,此代码只是维护方面的噩梦。我强烈建议你编写只执行一个特定任务的方法,否则代码会很快变得一团糟。
答案 1 :(得分:3)
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);
答案 2 :(得分:1)
@Word
不应该在sql查询中引用。
不确定为什么要尝试在sql查询的末尾添加连接。
答案 3 :(得分:0)
要调试它,请检查SqlCommand对象上的CommandText。在进一步阅读之前,你应该试试这个。
问题在于在参数化的字符串周围添加单引号。删除单引号,生活很美。 : - )
哦,你的conn是一个对象,需要一个逗号,而不是+。
答案 4 :(得分: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);
}
}
答案 5 :(得分:0)
String queryForUpdateCustomer =" UPDATE customer SET cbalance = @ txtcustomerblnc WHERE cname ='" + searchLookUpEdit1.Text +"'&#34 ;; 尝试 { 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();
}
答案 6 :(得分:0)
如果您无法使用上述规定的某些功能(由于我相信是较早版本的软件),也可以尝试以下方法:
using (var connection = _sqlDbContext.CreatSqlConnection())
{
using (var sqlCommand = _sqlDbContext.CreateSqlCommand())
{
sqlCommand.Connection = connection;
sqlCommand.CommandText = $"DELETE FROM excludes WHERE word = @word";
sqlCommand.Parameters.Add(
_sqlDbContext.CreateParameterWithValue(sqlCommand, "@word", word));
connection.Open();
sqlCommand.ExecuteNonQuery();
}
}
...
我是一名助理开发人员。因此,上面的“我相信”。