vb.net列表框和sql

时间:2014-04-24 10:43:06

标签: sql vb.net

我有4个列表框,我检查,其中的内容组成了放入sql语句的变量。这很好。问题是我必须检查所有的框。如果我留下任何sql语句中的框没有用。我试过添加""到了列表框但这没有用,它看起来很乱。我有办法解决这个问题。很多人感谢吉姆 这是我的代码:

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles MyBase.Load

    End Sub

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button1.Click
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim tables As DataTableCollection = ds.Tables
    Dim source1 As New BindingSource()
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String

    Dim aa As String = authorList.Text
    Dim bb As String = publisherList.Text
    Dim cc As String = yearpublishedList.Text
    Dim dd As String = genreList.Text

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source =C:\Documents and Settings\james\Desktop\Authors.accdb"
    con.ConnectionString = dbProvider & dbSource
    con.Open()
    sql = "SELECT * FROM books WHERE author = '" & aa & "' AND publisher = '" & bb &  "' AND yearpublished = '" & cc & "' AND genre = '" & dd & "' "


    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Authors")

    Dim view1 As New DataView(tables(0))
    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()

    'MsgBox("OPEN FOR LUNCH")
    'MsgBox("CLOSED FOR THE DAY")
    con.Close()
 End Sub
End Class

1 个答案:

答案 0 :(得分:2)

您正在将复选框解析为字符串,因此当您这样做时,您会得到:

1,2,3,4,5,6,7

如果您没有选中一个复选框,它将如下所示:

1,2,3,,5,6,7

所以SQL会中断

有几种方法可以解决这个问题,所以我不会尝试一个。但是,这应该有希望向你解释发生了什么以及为什么会发生这样你可以继续修复