必须声明标量变量“@ASIN”

时间:2017-02-17 20:49:09

标签: c# sql asp.net model-view-controller

我不明白错误信息。它是由标量变量得到的? @ASIN值应该存储在我为GetByAsin方法创建的id变量中。

//Gets one Music Detail
public MusicDescriptionModel GetByAsin(string id)
{
    using (IDbConnection db = connection)
    {
        string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +
            "FROM tblDescription WHERE id = @ASIN";
        //need to parameterize ID to avoid sql injection attacks.
        MusicDescriptionModel musicdesc = db.Query<MusicDescriptionModel>(sql, new { id }).SingleOrDefault();
        return musicdesc;
    }
}

3 个答案:

答案 0 :(得分:3)

您还没有为Dapper传递变量以绑定到@ASIN。尝试:

MusicDescriptionModel musicdesc
  = db.Query<MusicDescriptionModel>(sql, new { ASIN = id }).SingleOrDefault();

或尝试:

 string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +  
                       "FROM tblDescription WHERE id = @Id";

您无法使用其他名称,并希望它能够为您精心排列它们。他们需要匹配。

答案 1 :(得分:0)

string sql = "select ASIN, Title, Artist, Price, ReleaseDate,NumberDisks,label,DetailPageURL, Review" +  
                       "FROM tblDescription WHERE ASIN = @ASINSqlParam";
        //need to parameterize ID to avoid sql injection attacks.
        MusicDescriptionModel musicdesc = db.Query<MusicDescriptionModel>(sql, new { ASINSqlParam = id }).SingleOrDefault();

应该做的伎俩。

文档here可能会有所帮助。

答案 2 :(得分:0)

问题在于我的声明 我需要制作标量变量@id ASERE = @id