我正在尝试从sql数据库返回列表框中的行。我只希望返回行,其中3个字段匹配文本框值。目前,如果在3个文本框中输入ANYTHING,则列表框将返回所有行。如果没有输入任何内容,则不显示任何内容(该部分是预期/正确的行为)。 我的代码是:
Private Sub tireselect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_cn.ConnectionString = "Data Source=localhost;Initial Catalog=tires;Integrated Security=True"
m_cn.Open()
'Dim con As New SqlConnection()
'Dim cmd As String = "Select brand, model, width, aspect, rim, retail from dbo.tires"
Dim sqltext = "SELECT [ID] ,[brand] ,[model] ,[cost] ,[retail] ,[width] ,[aspect] ,[rim] FROM [tires].[dbo].[tires]"
Using sqlCon = New SqlConnection("Data Source=localhost;Initial Catalog=tires;Integrated Security=True")
sqlCon.Open()
Dim cmd = New SqlCommand(sqltext, sqlCon)
cmd.Parameters.AddWithValue("@tirewidth", (Int(TireLookup.txtWidth.Text)))
cmd.Parameters.AddWithValue("@tireaspect", (Int(TireLookup.txtAspect.Text)))
cmd.Parameters.AddWithValue("@tirerim", (Int(TireLookup.txtRim.Text)))
cmd.ExecuteNonQuery()
End Using
Dim m_da As New SqlDataAdapter("SELECT [ID] ,[brand] ,[model] ,[cost] ,[retail] ,[width] ,[aspect] ,[rim] FROM [tires].[dbo].[tires] where [width]=tirelookup.txtwidth.text AND [aspect]=tirelookup.txtwidth.text AND [rim]=tirelookup.txtrim.text", m_cn)
Dim myDataSet As New DataSet()
m_da.Fill(myDataSet, "dbo.tires")
Dim myDataTable As DataTable = myDataSet.Tables(0)
Dim tempRow As DataRow
For Each tempRow In myDataTable.Rows
ListBox1.Items.Add(tempRow("brand"))
ListBox2.Items.Add(tempRow("model"))
ListBox3.Items.Add(tempRow("width") & " / " & tempRow("aspect") & " / " & tempRow("rim"))
ListBox4.Items.Add("$ " & tempRow("retail") & " Tire ID=" & (tempRow("ID") - 1))
Next
End Sub
答案 0 :(得分:1)
您需要像这样更改谓词:
where [width]=@tirewidth
AND [aspect]=@tireaspect
AND [rim]=@tirerim