获取异常输入字符串的格式不正确
Getting Exception Input String was not in a correct format
我正在使用 ASP.NET C# 构建 windows 服务。
我正在尝试使用 MySqlCommand 对象从 MySQl 数据库进行调用。
我尝试从数据库中名为 tbl_patient 的 table 检索数据。
当我将以下查询传递给我的 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;
}
我猜问题出在我的 MySQl 连接器上,因为我看到 dataReader 有行(通过使用 Console.WriteLine(dataReader.HasRows 登录控制台,它显示为 true),
datareader 只是无法读取行,因为某些字段 lastSyncDate、DateModified 是 datetime 类型;
我所做的是删除 MySQL.data.dll 我通过解决方案资源管理器中的引用手动导入到我的项目中
并使用 nuget 包导入最新版本的 MySQl 连接器。
导入最新版本的 MySQl 连接器后,我检索了我的数据。
我正在使用 ASP.NET C# 构建 windows 服务。 我正在尝试使用 MySqlCommand 对象从 MySQl 数据库进行调用。 我尝试从数据库中名为 tbl_patient 的 table 检索数据。 当我将以下查询传递给我的 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;
}
我猜问题出在我的 MySQl 连接器上,因为我看到 dataReader 有行(通过使用 Console.WriteLine(dataReader.HasRows 登录控制台,它显示为 true), datareader 只是无法读取行,因为某些字段 lastSyncDate、DateModified 是 datetime 类型; 我所做的是删除 MySQL.data.dll 我通过解决方案资源管理器中的引用手动导入到我的项目中 并使用 nuget 包导入最新版本的 MySQl 连接器。 导入最新版本的 MySQl 连接器后,我检索了我的数据。