简体   繁体   English

选择使用小巧玲珑

[英]Selection using dapper

I have a problem with my query using dapper, whether someone imagine to help me and say where I'm doing the wrong, is currently showing me an error at the date when I put a breik point, how to correct it properly? 我的查询使用dapper有问题,是否有人想象帮助我并说出我做错了什么,目前在我提出错误点的日期显示错误,如何正确纠正? thank you all for help 谢谢大家的帮助

this is my current code 这是我目前的代码

public string GetBezeichnung(int LP, DateTime date)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection())
    {
        connection.ConnectionString = _ConnectionString;
        var output = connection.Query<string>("SELECT ZER_Bezeichnung FROM Z_ERFASSUNG WHERE ZER_LPE = " + LP + " AND ZER_Datum = " + date).FirstOrDefault();
        return output;
    }
}

and this is the result with which I get an error 这是我得到错误的结果

图像描述在这里

Try parameterizing the query. 尝试参数化查询。

public string GetBezeichnung(int LP, DateTime date)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(_ConnectionString))
    {
        connection.Open();
        string sql = @"
SELECT ZER_Bezeichnung 
FROM Z_ERFASSUNG 
WHERE ZER_LPE = @LP
  AND ZER_Datum = @date"
        var output = connection.Query<string>(sql, new { LP = LP, date = date }).FirstOrDefault();
        return output;
    }
}

I agree with @Parrish Husband, but the Second Paramater has to be "Date = date". 我同意@Parrish Husband,但第二个参赛者必须是“Date = date”。

In your example there are no ' around the date so AQL thinks its a bunch or things. 在你的例子中,没有'围绕日期,所以AQL认为它是一堆或一堆东西。

Changing it to 把它改成

var output = connection.Query<string>("SELECT ZER_Bezeichnung FROM Z_ERFASSUNG WHERE ZER_LPE = " + LP + " AND ZER_Datum = '" + date + "'").FirstOrDefault();

will make it work. 会使它工作。

Use the following line of code so that it will match the exact date. 使用以下代码行,以便它与确切的日期匹配。 if the time for the date is different then it will not fetch that records. 如果日期的时间不同,那么它将不会获取该记录。

var output = connection.Query<string>("SELECT ZER_Bezeichnung FROM Z_ERFASSUNG WHERE ZER_LPE = " + LP + " AND Cast( ZER_Datum as DATE) = Cast('" + date + "' as DATE)").FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM