首页 > 解决方案 > 使用具有多个参数化条件的 SELECT WHERE 查询检索表中的所有行

问题描述

我正在尝试从格式如下的 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)

标签: c#sqlsqlite

解决方案


根据您的评论,看起来正在发生的事情是 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)

这使用(用于 sqlite),但在 SQL 服务器中IFNULL可以实现相同的结果。ISNULL使用 IFNULL 将处理参数为空字符串 '' 或 NULL 的情况。


推荐阅读