你好。第一个代码片段将文本框值插入数据库并且效果很好:
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
片段可能是错误的吗?
谢谢你。