2011-12-03 101 views
1

我試圖在下面的查詢中返回成員標識。如果我運行查詢就像查詢我得到20,但是當我exectute代碼它返回一個零。我在這裏做錯了什麼?執行標準查詢碼問題

public int GetMemberID(string guid) 
    { 
     string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"]; 
     string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)"; 

     int memberId; 
     using (var connection = new SqlConnection(strConectionString)) 
     using (var command = new SqlCommand(StrSql, connection)) 
     { 
      command.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid; 
      memberId = (int)command.ExecuteScalar(); 
     } 

     return memberId; 

    } 
+0

我該如何接受答案? – CsharpBeginner

+0

http://stackoverflow.com/faq#howtoask - 閱讀本節的結尾。 – Jon

回答

1

​​變量不是int

command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid; 
0

您的參數@GuidID是一個Int類型嗎? 確保它是正確的。