SQL删除命令? [英] SQL delete command?

查看:26
本文介绍了SQL删除命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL中使用简单的DELETE语句遇到意外结果时遇到麻烦,似乎将单词添加到列表中了??一定是愚蠢的东西!但我看不到,尝试了几种不同的方式.所有相同的结果非常令人困惑.

I am having trouble with a simple DELETE statement in SQL with unexpected results , it seems to add the word to the list??. Must be something silly!. but i cannot see it , tried it a few different ways. All the same result so quite confused.

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

推荐答案

尝试删除单引号.另外,为什么还要用连接对象( .. word ='@ word'"+ conn )连接SQL字符串?

Try removing the single quotes. Also why are you concatenating your SQL string with a connection object (.. 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连接池,以便可以重复使用.

Notice also that because the connection is wrapped in a using block you don't need to Close it in a finally statement. The Dispose method will automatically call the .Close method which will return the connection to the ADO.NET connection pool so that it can be reused.

另一句话是,此 IncludeWord 方法可以完成许多事情.它发送SQL查询以删除记录,它在GUI上更新一些文本框,并绑定一些列表=>这样的方法应该分开分开,这样每个方法都有其特定的职责.否则,此代码只是维护方面的噩梦.我强烈建议您编写仅执行单个特定任务的方法,否则代码很快就会变成一团糟.

Another remark is that this IncludeWord method does far to many things. It sends SQL queries to delete records, it updates some textboxes on the GUI and it binds some lists => methods like this should be split in separate so that each method has its own specific responsibility. Otherwise this code is simply a nightmare in terms of maintenance. I would very strongly recommend you to write methods that do only a single specific task, otherwise the code quickly becomes a complete mess.

这篇关于SQL删除命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆