我正在努力解決SQL
中的代碼VB
但我遇到問題我有一個簡單的數據庫與表管理員UserName
和Password
列。如何在Vb 2010中使用SQL中的參數(Web開發人員)
我希望能夠從文本框中讀取數據,然後將其輸入到一個SQL字符串中… SQL字符串的工作原理(我測試了它),我可以用簡單的SELECT
語句得到它,但我似乎無法讓SQL讀取我的參數。
幫助?
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call Password_Check(txtTestInput.Text)
End Sub
Public Sub Password_Check(ByVal Answer As String)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim parameter As New SqlParameter("@Username", Answer)
Try
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database1ConnectionString1").ConnectionString
con.Open()
cmd.Connection = con
cmd.CommandText = " SELECT Password FROM Admin WHERE (UserName = @Username)"
cmd.Parameters.Add(parameter)
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
Dim sothing As String
sothing = lrd("Password").ToString
If lrd("Password").ToString = txtPassword.Text Then
lblTestData.Text = "passwordSuccess"
ElseIf lrd("Password").ToString <> txtPassword.Text Then
lblTestData.Text = "passwordFail...:("
End If
End While
Catch ex As Exception
lblTestData.Text = "Error while retrieving records on table..." & ex.Message
Finally
con.Close()
End Try
End Sub
它是否進入While lrd.Read()Loop,如果是這樣的話lrd(「Password」)的值是什麼?ToString – PatFromCanada
你似乎無法讓SQL讀取你的參數,那麼它看起來如何其實呢?拋出的異常? – deerchao