我是C#的新手,我正在编写一个桌面应用程序以读取MS-Access发票表,以便可以对其进行进一步处理。但是即使表中有行,我也无法获取“ reader.HasRows”语句来从SELECT语句返回行。每次运行该应用程序时,都会显示“未找到发票记录”消息框。
我已经在这个论坛上研究并阅读了其他类似问题的文章,但似乎没有一个能解决我的特定问题。我也在互联网上搜索了没有结果的解决方案。有人可以帮我发现我在做什么错吗?
private void AddContactsAcconutNumberToInvoices()
{
//Use a variable to hold the SQL statement.
string inputString = "SELECT Invoices.[Job_Name], * FROM Invoices " +
"WHERE Invoices.[Account_Number] = Null";
try
{
//Create an OleDbCommand object and pass in the SQL statement and OleDbConnection object
OleDbCommand cmd = new OleDbCommand(inputString, conn);
//Send the CommandText to the connection, and then build an OleDbDataReader.
OleDbDataReader reader = cmd.ExecuteReader();
// Read through the database and test the integrity of the data
if (reader.HasRows)
{
while (reader.Read())
{
var jobname = reader.GetString(0);
var contactsQuery = "SELECT Account_Number FROM CONTACTS WHERE Contacts.Full_Name = " + jobname;
MessageBox.Show("Contacts query contatins: " + contactsQuery);
}
}
else { MessageBox.Show("No Invoice records found"); }
}
catch (Exception ex)
{
error_message = ex.Message;
MessageBox.Show(error_message);
}
}
答案 0 :(得分:5)
问题出在您的查询,尤其是= Null
。您需要使用Is Null
:
string inputString = "SELECT Invoices.[Job_Name], * FROM Invoices " +
"WHERE Invoices.[Account_Number] Is Null";
不仅对于Access,而且对于SQL(该语言),这都是正确的。
此处专门针对Access:https://support.office.com/en-us/article/examples-of-query-criteria-3197228c-8684-4552-ac03-aba746fb29d8#bm1