我有一个数据网格控件,我试图根据在页面上输入参数的SQL查询来填充。我一直在得到必须声明标量变量@ epa1,所以我有点迷失在哪里去。 VB代码是:
Dim DT As DataTable
Dim DS As New DataSet()
Dim conn As New SqlConnection("Data Source=azda-sql1a;Initial Catalog=pesticide;User ID=sa;Password=Sql@dm!n")
Dim cmd As New SqlCommand
Dim query As String
Dim DA As SqlDataAdapter
query = "select epa1, epa2, dist_nr as epa3, ai1, rei, rup from products where epa1 = @epa1 and epa2 = @epa2 and dist_nr = @epa3"
cmd.CommandType = CommandType.Text
cmd.CommandText = query
cmd.Connection = conn
cmd.Parameters.AddWithValue("@epa1", txtEPA1.Text)
cmd.Parameters.AddWithValue("@epa2", txtEPA2.Text)
cmd.Parameters.AddWithValue("@epa3", txtEPA3.Text)
'\search for pesticides based on epa numbers
'select BrandName,ActiveIngredient1 from NewProductsList where epa1=# and epa2=# and epa3=#
If Me.txtEPA1.Text = "" Or Me.txtEPA2.Text = "" Or Me.txtEPA3.Text = "" Then
Me.lblProductErrors.Text = "Please enter in all EPA numbers for accurate search, when in doubt enter Zero"
End If
cmd.Connection.Open()
DA = New SqlDataAdapter(query, conn)
DA.Fill(DS)
DT = DS.Tables(0)
conn.Close()
所以我假设所有3个变量,@ epa1,@ epa2和@ epa3都没有被声明,但是再次,它们就是。有人可以帮忙吗?谢谢......每个人都有一个美好的圣诞节!
答案 0 :(得分:3)
您可以在Command
上设置参数,但是使用query
(不含参数)来初始化DataAdapter
。
因此,不是使用查询初始化DataAdapter:
DA = New SqlDataAdapter(query, conn)
使用Command(已设置参数)初始化它:
DA = New SqlDataAdapter(cmd)