多个where子句SQL Server

时间:2014-10-16 19:26:58

标签: c# sql sql-server

我正在编写此查询以检查重复项,但无法获得多个工作。 ","附近的语法错误就是我得到的。

private static bool duplicate(Dictionary<TableKey, string> entry)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        // TODO: Make changes to this command when new additions/modifications to the DB structure occur  
        string commandText = @"Select Barcode, FullName, Location, Notes, Category From Inventory " +
                             @"Where Barcode = @barcode, FullName = @fullname, Location = @location, " +
                             @"Notes = @notes, Category = @category";

        SqlCommand command = new SqlCommand(commandText, connection);

        command.Parameters.AddWithValue("@barcode", entry[TableKey.Barcode]);
        command.Parameters.AddWithValue("@fullname", entry[TableKey.FullName]);
        command.Parameters.AddWithValue("@location", entry[TableKey.Location]);
        command.Parameters.AddWithValue("@notes", entry[TableKey.Notes]);
        command.Parameters.AddWithValue("@category", entry[TableKey.Category]);

        command.Connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
            return true;
        else
            return false;
    }

这是我的问题:

@"Where Barcode = @barcode, FullName = @fullname, Location = @location, " +
                             @"Notes = @notes, Category = @category";

我怎么会有多个人?我已经尝试了&#34;和&#34;和&#34; AND&#34;

2 个答案:

答案 0 :(得分:3)

这应该可以正常工作。

@"Where Barcode = @barcode AND FullName = @fullname AND Location = @location AND " +
      @"Notes = @notes AND Category = @category";

查找重复项

SELECT COUNT(*) as cnt, * 
From Inventory
GROUP  BY Barcode, FullName, Location, Notes, Category
HAVING COUNT(*) > 1

答案 1 :(得分:0)

你应该使用

  

  

OR

条件

下面的

是使用AND

的示例
"Where Barcode = @barcode AND FullName = @fullname AND Location = @location AND " +
                             @"Notes = @notes AND Category = @category";