我正在尝试根据中的所选代码选择客户的姓名ComboBox
。当我运行以下代码时,我收到一条错误消息,指出一个或多个必需参数没有任何值。
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = @code"
Dim cmd As New OleDb.OleDbCommand(sql, con)
cmd.Parameters.Add("@code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
但是,当我运行相同的查询但不带参数时,它运行良好。
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = '" & cmbCustomer.Text & "'"
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
第一种方式我真的错过了某些东西吗?如果没有,为什么这种方式行不通?
这是因为 OleDbDataAdapter
您将设置DataAdapter
为接受sql
,和连接的字符串con
,这意味着cmd.Parameter
不会随其传递。
因此,您的代码将在寻找da.SelectCommand.Parameters.Add
。
你要么需要
da.SelectCommand.Parameters.Add("@Code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
或者
Dim da As New OleDb.OleDbDataAdapter(cmd)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句