为什么我的参数化ADO查询出错?

时间:2010-12-17 04:15:04

标签: vb.net ado.net sqlcommand sqlparameters

Dim txtFName As TextBox = CType(Wizard1.FindControl("txtFName"), TextBox)
Dim txtLName As TextBox = CType(Wizard1.FindControl("txtLName"), TextBox)

Dim MyDbConnection As New SqlConnection(ConfigurationManager.ConnectionStrings.Item("Journeyeast Connection String").ToString)
MyDbConnection.Open()

Dim SQL = "INSERT INTO Registration(FName, LName) VALUES (@FName, @LName)"

Dim MySqlCommand As New SqlCommand(SQL, MyDbConnection)
MySqlCommand.Parameters.Add("@FName", SqlDbType.NChar, 10, txtFName.Text)
MySqlCommand.Parameters.Add("@LName", SqlDbType.NChar, 10, txtLName.Text)

MySqlCommand.ExecuteNonQuery()

我收到以下错误:

  

参数化查询'(@ FName nchar(10),@ LName nchar(10))INSERT INTO Registration(FNam'期望参数'@FName',未提供。

我做错了什么?

BTW,我知道nchar(10)不是名字和姓氏的好选择。

1 个答案:

答案 0 :(得分:3)

因为您没有提供参数值。

看看:

Dim SQL = "INSERT INTO Registration(FName, LName) VALUES (@FName, @LName)"

Dim MySqlCommand As New SqlCommand(SQL, MyDbConnection)
MySqlCommand.Parameters.AddWithValue("@FName",txtFName.Text)
MySqlCommand.Parameters.AddWithValue("@LName",txtLName.Text)

MySqlCommand.ExecuteNonQuery()

编辑:

MySqlCommand.Parameters.Add("@FName", SqlDbType.NChar, 10).Value = txtFname.Text
MySqlCommand.Parameters.Add("@LName", SqlDbType.NChar, 10).Value = txtFname.Text