数据存在,但我的程序说';不存在该行/列的数据'
本文关键字:数据 不存在 程序 我的 存在 | 更新日期: 2023-09-27 17:58:16
private void recordFromTable()
{
String firstName = Environment.UserName.Split('.')[0];
String lastName = Environment.UserName.Split('.')[1];
String query = "SELECT LastUpdate FROM " + "EmployeeContact" + " WHERE EmployeeLastName='" + lastName + "' AND EmployeeFirstName='" + firstName + "'";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
connection.Open();
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
labelLastUpdated.Text = reader.GetString(0).Trim();
}
}
}
} catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
抛出的异常为该行/列不存在任何数据
我打开Access数据库并运行相同的查询(我的检查器在调试时显示(,然后从数据库中返回一行。
Access中的所有字段都是"短文本">
如果您确定数据存在,在我看来,您的firstName或lastName变量可能没有被分配您认为是或希望它们分配的值。
很可能没有满足指定的WHERE
条件的记录,因此也没有满足所述异常。验证运行此
SELECT COUNT(*) FROM
EmployeeContact
WHERE EmployeeLastName= lastName
AND EmployeeFirstName=firstName;
您还可以将查询更改为此
String query = "SELECT Nz(LastUpdate, 'NO DATA PRESENT') LastUpdate FROM " + "EmployeeContact" + " WHERE EmployeeLastName='" + lastName + "' AND EmployeeFirstName='" + firstName + "'";