2013-02-18 74 views
1

我有一個麻煩,準備帶有2個參數的查詢,在VB.NET在VB.NET中準備一個查詢

這是我的代碼:

Dim username As String = loginUsername.Value 
Dim password As String = EncryptMD5standard(loginPassword.Value) 
Dim valid As Boolean = False 
Dim connectionString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString 
     Dim queryString As String = "SELECT id, user_name, role FROM users WHERE user_name = '@user' AND paswd = '@pass'" 
     Dim ds As New DataSet() 
     Try 
      Using connection As New SqlConnection(connectionString) 
       Dim command As New SqlCommand(queryString, connection) 
       connection.Open() 
       command.CommandText = queryString 
       command.Parameters.Add("@user", SqlDbType.NVarChar, 15).Value = username 
       command.Parameters.Add("@pass", SqlDbType.NVarChar, 32).Value = password 
       Dim adapter As New SqlDataAdapter() 
       adapter.SelectCommand = command 

       adapter.Fill(ds, "login") 
       If ds.Tables("login").Rows.Count > 0 Then 
        valid = True 
       End If 
      End Using 
     Catch ex As Exception 
      errorLabel.Text = DirectCast(GetLocalResourceObject("erroreDB"), String) & ": " & ex.ToString 
     End Try 

但是,這樣做我valid值始終爲「假」,所以它失敗的行數。 我用了一些調試,看起來像我的表login裏面ds是空的。 查詢工作原理,我在SQLServer中手動嘗試替換參數,我不明白爲什麼我有空的結果。

我做錯了什麼?

回答

3

您不需要用單引號包裝您的參數,因爲參數系統會處理這些問題。

+0

哇,這很快,謝謝你現在它的作品! :d – HypeZ 2013-02-18 15:20:41