我正在使用 ASP.NET C# 构建 Windows 服务。我正在尝试使用 MySqlCommand 对象从 MySQl 数据库中进行调用。我尝试从数据库中名为 tbl_patient 的表中检索数据。当我将以下查询传递给 MySQlCommand 对象时:
SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient
它有效,这意味着我检索数据,但是,当我将查询更改为该查询时:
SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient WHERE lastSyncDate < DateModified
我不断收到错误:输入字符串的格式不正确,我猜错误是由于数据读取器尝试读取查询返回的行时 tbl_patient 中的这些日期时间字段,有人可以帮我解决这个问题吗?
这是我的方法的完整代码片段,它试图从 tbl_patient 获取记录以便更好地了解我的情况,我将它添加到引发异常的查询中:
public static List<Tbl_Patient> getPatients()
{
List<Tbl_Patient> patients = new List<Tbl_Patient>();
string query = "SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient WHERE lastSyncDate < DateModified"; // This query passed as cmdText in my MySqlCommand object throws exception
MySqlConnection connection = DBUtils.GetDBConnection();
connection.Open();
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
try
{
while (dataReader.Read())
{
Tbl_Patient patient = new Tbl_Patient();
patient.Id = (long)(dataReader["Id"]);
patient.ID_Institution = Convert.ToInt64(dataReader["ID_Institution"]);
patient.AdresseDomiciliaire = dataReader["AdresseDomiciliaire"].ToString();
patient.PersonneDeContact = dataReader["PersonneDeContact"].ToString();
patient.Telephone = dataReader["Telephone"].ToString();
patients.Add(patient);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message); // Here is where the exception comes from
}
dataReader.Close();
connection.Close();
return patients;
}