我正在使用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中的这些datetime字段引起的,有人可以帮我解决这个问题吗?
这是我的方法的完整代码段,为了更好地了解我的情况,该方法试图从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;
}
我猜问题出在我的MySQl Connector上,因为我看到dataReader有行(通过使用Console.WriteLine(dataReader.HasRows)登录控制台,它显示为true),由于某些字段lastSyncDate,datareader不能读取行,DateModified为datetime类型;我要做的是删除通过解决方案资源管理器中的引用在项目中手动导入的MySQL.data.dll,并导入具有nuget包的MySQl连接器的最新版本。导入最新版本的MySQl连接器后,我将检索数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句