我正在尝试使用Dapper
从我的ODBC
驱动程序查询表。我要运行的特定查询需要返回其DateTime
值大于该查询的传递参数的行。
这是我尝试过的:
string delta = "05/02/2019 08:41:37";
DateTime deltaDt = DateTime.Parse(delta);
using (IDbConnection connection = new OdbcConnection(/**Connection string**/))
{
connection.Open();
try
{
string query = "SELECT * FROM MyTable WHERE Date > @Delta";
IEnumerable<object> records = connection.Query<object>(query, new { Delta = deltaDt });
Console.WriteLine(records.Count());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
但是,由于上述原因,我得到以下错误:
{"ERROR [22018] Cannot convert non-numeric data"}
然后我尝试使用不带IDBConnection
的{{1}}来查看我是否做错了什么,但是如果我执行以下操作,查询就可以正常工作:
Dapper
使用string delta = "05/02/2019 08:41:37";
DateTime deltaDt = DateTime.Parse(delta);
using (IDbConnection connection = new OdbcConnection(/**Connection string**/))
{
connection.Open();
string odbcQuery = "SELECT * FROM MyTable WHERE Date > ?";
IDbCommand command = connection.CreateCommand();
IDbDataParameter parameter = command.CreateParameter();
parameter.Value = deltaDt;
command.Parameters.Add(parameter);
command.CommandText = odbcQuery;
System.Data.IDataReader reader = command.ExecuteReader();
while (reader.Read())
{
object[] values = new object[1];
reader.GetValues(values);
console.log(values);
}
}
查询绑定DateTime
时,我做错什么了吗?据我所见,无论我使用Dapper
查询还是Dapper
,该值都是相同的。我也尝试了以下方法,但仍然遇到相同的问题:
IDbDataParameter
答案 0 :(得分:1)
以这种方式尝试:
SELECT * FROM MyTable WHERE Date > ?Delta?
然后
IEnumerable<object> records = connection.Query<object>(query, new { Delta = deltaDt });