【问题标题】:SQL Server And C# WinForms errorSQL Server 和 C# WinForms 错误
【发布时间】:2011-07-15 18:09:39
【问题描述】:

我在表单加载时调用这个方法 GetProducts(" "); 此查询在 sql 中运行良好。它一直在工作,直到我添加了 WHERE 当我在这一行上使用调试器时 >> SqlDataReader myReader = cmd.ExecuteReader(); 谁能给我一些建议?

 public void GetProducts(string find)
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("SELECT ID, BarCode, ArtNumber, ProductName, Price, SelfPrice, PriceWithOutAWD, TotalSelfPrice, UnitsInStock, " +
                                                " Comment, InputDateTime, InputQuantity, Margin, CategoryName, TypeName, ExpDate FROM GetProducts"+
                                                "WHERE BarCode LIKE '%@F%' OR ArtNumber LIKE '%@F%' OR ProductName LIKE '%@F%' OR Price LIKE '%@F%' OR Comment LIKE '%@F%' ", 
                                                new SqlConnection(Program.ConnectionString)))
            {
                cmd.Parameters.AddWithValue("@F", find);
                cmd.Connection.Open();

                SqlDataReader myReader = cmd.ExecuteReader();
                while (myReader.Read())
                {

                    ProductTable.Rows.Add
                        (
                        (int)myReader["ID"],
                        myReader["BarCode"].ToString(),
                        myReader["ArtNumber"].ToString(),
                        myReader["ProductName"].ToString(),
                        (decimal)myReader["Price"],
                        (decimal)myReader["SelfPrice"],
                        (decimal)myReader["PriceWithOutAWD"],
                        myReader["TotalSelfPrice"].ToString(),
                        myReader["UnitsInStock"].ToString(),
                        myReader["Comment"].ToString(),
                        myReader["InputDateTime"].ToString(),
                        myReader["InputQuantity"].ToString(),
                        myReader["Margin"].ToString(),
                        myReader["CategoryName"].ToString(),
                        myReader["TypeName"].ToString(),
                        myReader["ExpDate"].ToString()
                        );
                }
                cmd.Connection.Close();
            }
        }
        catch (Exception)
        {
            MessageBox.Show(Program.MsgError1, "Acid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

    }

【问题讨论】:

  • 你能指定你得到的错误吗

标签: c# sql sql-server winforms


【解决方案1】:
... FROM GetProducts"+
"WHERE BarCode LIKE ...

应该是(注意GetProductsWHERE之间的额外空格)

... FROM GetProducts "+
"WHERE BarCode LIKE ...

也不要使用

WHERE BarCode LIKE '%@F%'

任意使用

WHERE BarCode LIKE '%' + @F + '%' 

或者让参数值包含通配符并使用

cmd.Parameters.AddWithValue("@F", "%" + find + "%"); 
 ...
WHERE BarCode LIKE @F

如果您总是使用前导通配符进行搜索,那么您可能应该使用

WHERE CHARINDEX(@F,BarCode) > 0

无论如何,您的查询将无法使用索引,如果用户搜索包含在模式语法中具有特殊含义的字符的子字符串,此方法可避免出现问题。

但是,从您正在搜索的列数量和前导通配符来看,您可能应该为此使用全文索引。

【讨论】:

  • @Acid 需要更多细节!您还需要在WHERE 之前添加一个空格
【解决方案2】:

看起来您正在尝试引用字符串文字内的参数:

WHERE BarCode LIKE '%@F%'

在这种情况下,我认为您无法连接一些 sql:

" WHERE BarCode LIKE '%" + find +  "%' "

只要确保您避开 find 中的任何撇号(并保护自己免受SQL Injection 的影响):

find = find.Replace("'","''");

【讨论】:

  • 删除了之前的评论。正如我所见,您 are 实际上建议连接可执行字符串。 OP 并没有坚持这样做。我的答案中的方法肯​​定有效(并且不会导致潜在的 SQL 注入问题)
  • @Martin - 我不知道这会起作用。你的方法肯定更健壮。
猜你喜欢
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多