2017-02-17 71 views
0

我不明白錯誤消息。標量變量是什麼? @ASIN值應該存儲在爲GetByAsin方法創建的id變量中。必須聲明標量變量「@ASIN」

//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; 
     } 
    } 
+0

如何@ASIN得到您的SQL字符串設定? – Jake

+0

它在id = @ASIN –

+0

@mason擊敗了我。 – Jake

回答

2

您尚未傳遞變量以使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"; 

不能使用不同的名稱,並期望它奇蹟般地排隊他們爲你。他們需要匹配。

+0

我試過了,但它給了我一個錯誤消息,說ID是一個無效的列 –

+0

@TimothyWong那麼看看'tblDescription',看看你應該過濾哪一列。我無法訪問數據庫以查看您的表上定義了哪些列。 – mason

+0

我想讓我想傳遞的字符串id等於ASIN。這就是爲什麼把id = @ASIN放在我的where語句中。問題是:MusicDescriptionModel musicdesc = db.Query (sql,new {id})。SingleOrDefault(); –

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(); 

應該這樣做。

Docs here可能會有所幫助。

0

的問題是在我的WHERE語句 我需要做的標量變量@id WHERE ASIN = @id