c# - 使用具有多个参数化条件的 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)
解决方案
根据您的评论,看起来正在发生的事情是 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 的情况。
推荐阅读
- sql - 涉及 SELECT FOR UPDATE 的死锁
- github - 如何从 github graphql 查询中获取拉取请求的默认合并方法?
- c++ - 查找由数字 0 和 1 组成的数字“m”的算法说明,该数字可以被数字 n 整除
- r - Shiny:从 fileInput 获取准确的数据路径,而不是临时数据路径
- mysql - MS 通过 odbc 访问 mySql 连接慢
- typescript - 使用静态成员定义类型参数
- java - JAX-RS 2.1.1 响应构建抛出错误
- php - 如何使用 PHP 检测按钮单击并打开新页面?
- java - 关于 Spring 2.5 和 Java 6 的问题
- python - Discord py如何检测特定的成员状态