以日期时间查询小巧玲珑[复制]

问题描述 投票:2回答:1

这个问题已经在这里有一个答案:

我试图用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没有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。我也曾尝试以下,但仍得到了同样的问题:

string query = "SELECT * FROM MyTable WHERE Date > @Delta";
DynamicParameters dp = new DynamicParameters();
dp.Add("@Delta", deltaDt, DbType.DateTime);
IEnumerable<object> records = connection.Query<object>(query, dp);
c# dapper
1个回答
1
投票

尝试以这种方式:

SELECT * FROM MyTable WHERE Date > ?Delta?

然后

 IEnumerable<object> records = connection.Query<object>(query, new { Delta = deltaDt });
© www.soinside.com 2019 - 2024. All rights reserved.