简体   繁体   中英

SQLite VB.net Query

im trying to learn coding using VB.net, I have a simple CRUD application(VB.net and Sqlite) where the data is show in a grid box. everything works except the searching of the data.

i want to seach the DB based on the column name in which the user chooses in the combobox.

original code:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    ' search functions
    connect()
    Dim da As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE name like '%" & TextBox1.Text & "%'", connection)
    Dim dt As New DataTable

    da.Fill(dt)
    DataGridView1.DataSource = dt
    connection.Clone()
    da.Dispose()
End Sub

the original code works, but i wanted to have an option to search based on the column name, so i tried this

modified code:

   Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    ' search functions
    connect()
    Dim da As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE name like '%" & TextBox1.Text & "%'", connection)
    Dim db As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE country like '%" & TextBox1.Text & "%'", connection)
    Dim dt As New DataTable
    If ComboBox1.SelectedValue = "name" Then
        da.Fill(dt)
    ElseIf ComboBox1.SelectedValue = "country" Then
        db.Fill(dt)

    End If


    DataGridView1.DataSource = dt
    connection.Clone()
    da.Dispose()
End Sub

i can't get it to work. i know my main problem is

     Dim da As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE name like '%" & TextBox1.Text & "%'", connection)

but i got no more idea on how to do it. any help is much appreciated, thank you.

Found the Answer, thank you everyone for the Help

    If ComboBox1.SelectedIndex = 0 Then
        Dim da As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE name like '%" & TextBox1.Text & "%'", connection)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        connection.Close()
        da.Dispose()
    Else
        Dim da As New SQLiteDataAdapter("SELECT * FROM tbl_biodata WHERE country like '%" & TextBox1.Text & "%'", connection)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        connection.Close()
        da.Dispose()

    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM