使用具有多个参数化条件的SELECT WHERE查询检索表中的所有行

时间:2018-12-05 10:48:39

标签: c# sql sqlite

我正在尝试从格式如下的SQLite数据库中检索一些数据:

Customers

id | firstName | lastName | address | phone | email | notes

我有几个TextBoxes和一个DataGridView来显示来自客户的数据。我想做的是检索并在DataGridView中显示与任何TextBox的内容匹配的任何行。代码如下,但是我无法弄清楚我的SQL语句在做什么:

public static DataTable RecoverCustomer(Customer customer)
    {
        var connection = new SQLiteConnection("Data Source = prova.sqlite; Version=3;");

        var command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM customers WHERE firstName = @firstName OR lastName = @lastName OR address = @address OR phone = @phone OR email = @email OR notes = @notes";
        command.Parameters.AddWithValue("@firstName", customer.FirstName);
        command.Parameters.AddWithValue("@lastName", customer.LastName);
        command.Parameters.AddWithValue("@address", customer.Address);
        command.Parameters.AddWithValue("@phone", customer.Phone);
        command.Parameters.AddWithValue("@email", customer.Email);
        command.Parameters.AddWithValue("@notes", customer.Notes);

        var dataAdapter = new SQLiteDataAdapter();
        var dataTable = new DataTable();

        connection.Open();
        dataAdapter.SelectCommand = command;
        dataAdapter.Fill(dataTable);
        connection.Close();

        return dataTable;
    }

我的问题是此代码返回的行数比预期的多。可能是因为在添加新客户时,我没有检查是否存在任何空字段,所以当我使用此代码搜索客户时会返回这些行吗?如果是,我该如何缓解呢?这是我用来向表中添加新客户的代码:

public static void CreateCustomer(Customer customer)
    {
        var connection = new SQLiteConnection("Data Source = prova.sqlite; Version=3;");
        var command = connection.CreateCommand();
        command.CommandText = "INSERT INTO customers (firstName, lastName, address, phone, email, notes) VALUES (@firstName, @lastName, @address, @phone, @email, @notes)";
        command.Parameters.AddWithValue("@firstName", customer.FirstName);
        command.Parameters.AddWithValue("@lastName", customer.LastName);
        command.Parameters.AddWithValue("@address", customer.Address);
        command.Parameters.AddWithValue("@phone", customer.Phone);
        command.Parameters.AddWithValue("@email", customer.Email);
        command.Parameters.AddWithValue("@notes", customer.Notes);

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
    }

编辑

由于@Haldo,我更正了与空白匹配的SQL语句,它可以正常工作。正确的声明是:

SELECT * FROM customers WHERE (IFNULL(@firstName, '') <> '' AND firstName = @firstName) OR (IFNULL(@lastName, '') <> '' AND lastName = @lastName) OR (IFNULL(@address, '') <> '' AND address = @address) OR (IFNULL(@phone, '') <> '' AND phone = @phone) OR (IFNULL(@email, '') <> '' AND email = @email) OR (IFNULL(@notes, '') <> '' AND notes = @notes)

2 个答案:

答案 0 :(得分:0)

根据您的注释,看起来好像SQL Select语句正在匹配空值/空值,这就是为什么返回的行比预期多的原因。

仅当参数具有值时,才想匹配结果。因此,将select语句修改为以下内容应该可以:

SELECT * FROM customers 
    WHERE (IFNULL(@firstName, '') <> '' AND firstName = @firstName) 
    OR (IFNULL(@lastName, '') <> '' AND lastName = @lastName)
    OR (IFNULL(@address, '') <> '' AND address = @address)
    OR (IFNULL(@phone, '') <> '' AND phone = @phone)
    OR (IFNULL(@email, '') <> '' AND email = @email)
    OR (IFNULL(@notes, '') <> '' AND notes = @notes)

这使用IFNULL(对于sqlite),但是在SQL Server中使用ISNULL可以达到相同的结果。使用IFNULL将处理参数为空字符串''或NULL的情况。

答案 1 :(得分:0)

如果我做对了,您想返回至少与传递的参数之一匹配的行。 因此,首先应添加一个检查,以确保至少有一个不为null的参数。在我看来,它应该在Customer构造函数中(因为它必须拥有一个具有所有空参数的客户,这意味着什么?)。 Java constructor style: check parameters aren't null 然后,您可以将select语句更改为如下所示:

SELECT * FROM customers
WHERE ( firstName = @firstName AND @firstName IS NOT NULL)
OR ( lastName = @lastName AND @lastName IS NOT NULL)
OR ( address = @address AND @address IS NOT NULL)
OR ( phone = @phone AND @phone IS NOT NULL)
OR ( email = @email AND @email IS NOT NULL)
OR ( notes = @notes AND @notes IS NOT NULL)