嗨。第一代码片段插入文本框的值到数据库和它工作得很好:使用AddWithValue插入 - 引发错误?
Public Sub InsertintoTable(ByVal username As String, ByVal password As String)
Dim adp As New SqlCommand("INSERT INTO [users_tbl] (usr_username,usr_password) values ('" & username & "','" & password & "')", con)
adp.ExecuteNonQuery()
End Sub
相反,我想用AddWithValue,所以我试试这个:
Public Sub InsertintoTable(ByVal username As String, ByVal password As String)
Using adp As New SqlCommand("INSERT INTO [users_tbl] (usr_username, usr_password) values (@username, @password)", con)
adp.Parameters.AddWithValue("@usr_username", username)
adp.Parameters.AddWithValue("@usr_password", password)
adp.ExecuteNonQuery()
End Using
End Sub
但不幸的是它会抛出一个错误:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@username".
为什么错误?可能我的AddWithValue
代码段错了?
谢谢。
未使用的方法我自己,但看起来像你应该有'AddWithV alue(“@ username”,username)''而不是'“@user_username”' – freefaller 2013-03-07 14:19:16
谢谢你是对的。无论如何,我不知道为什么AddWithValue有很多参数,这使事情变得复杂。 – compliance 2013-03-07 19:08:52