2013-03-07 33 views
1

我有小巧的工作正常,但它是不安全的,因爲我沒有使用參數,我怎麼能最好的把我的小巧變量變成參數,例如這是我有工作的非參數化代碼。 。mvc3 dapper參數問題

var getinfo = sqlConnection.Query<test>("Select name,location from tests where location="+ myplace).FirstOrDefault(); 

MyPlace酒店是把用戶的信息,現在,當我試圖設定參數

var getinfo = sqlConnection.Query<test>("Select name,location from tests where location='@location'", new {location = myplace}).FirstOrDefault(); 

我得到絕對沒有就返回,但沒有錯誤消息的代碼一個文本框。我可以在這裏丟失什麼,或什麼是參數化變量的最佳方式。

回答

2

您不需要在參數周圍放置單引號。希望這可以幫助。

var getinfo = sqlConnection.Query<test>("Select name,location from tests where [email protected]", new {location = myplace}).FirstOrDefault(); 
+0

謝謝你多數民衆贊成在工作 – user1949387 2013-03-07 23:06:32