如何将SQLite数据库读入DataGridView对象

时间:2014-04-24 18:43:48

标签: vb.net sqlite system.data.sqlite

我正在使用VS 2013编写VB程序。我正在使用SQLite.org的System.Data.SqLite.dll中的方法。我可以将我的数据库精确地读入ListBox对象。我发布了我正在使用的代码。我想要做的是将此数据发送到DataGridView对象。我没有运气正确。

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim f As New OpenFileDialog
    f.Filter = "SQLite 3 (*.db)|*.db|All Files|*.*"
    If f.ShowDialog() = DialogResult.OK Then
        Dim SQLconnect As New SQLite.SQLiteConnection()
        Dim SQLcommand As SQLiteCommand
        SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
        SQLconnect.Open()
        SQLcommand = SQLconnect.CreateCommand
        SQLcommand.CommandText = "SELECT address, date, body FROM sms ORDER BY date DESC"
        Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()

        lst_records.Items.Clear()

        While SQLreader.Read()
            lst_records.Items.Add(String.Format("address = {0}, date = {1}, body = {2}",      SQLreader(0), SQLreader(1), SQLreader(2)))
         End While

        SQLcommand.Dispose()
        SQLconnect.Close()
    End If
End Sub

5 个答案:

答案 0 :(得分:3)

我在StackOverflow上发现了一些类似的问题,但不够贴近发布。很抱歉将您转到其他网站。 http://cplus.about.com/od/howtodothingsinc/ss/How-To-Use-Sqlite-From-Csharp_2.htm 这是来自上述链接的复制/粘贴。答案是使用SQLLiteConnection和SQLLiteDataAdapter。下面是C#,但很容易转换为VB。

private void btngo_Click(object sender, EventArgs e)
 {
     const string filename = @"C:\cplus\tutorials\c#\SQLite\About.sqlite";
     const string sql = "select * from friends;";
     var conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;") ;
     try
     {
       conn.Open() ;
       DataSet ds = new DataSet() ;
       var da = new SQLiteDataAdapter(sql, conn) ;
       da.Fill(ds) ;
       grid.DataSource = ds.Tables[0].DefaultView;
     }
     catch (Exception)
     {
 throw;
     }
 }

答案 1 :(得分:1)

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim f As New OpenFileDialog
    f.Filter = "SQLite 3 (*.db)|*.db|All Files|*.*"
    If f.ShowDialog() <> DialogResult.OK Then Exit Sub

    lst_records.Items.Clear()
    Using SQLconnect As New SQLiteConnection("Data Source=" & f.FileName & ";"), _
          SQLcommand As New SQLiteCommand("SELECT address, date, body FROM sms ORDER BY date DESC")

        SQLconnect.Open() 
        Using SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
            MyDataGridView.DataSource = SQLreader       
        End Using
    End Using
End Sub

答案 2 :(得分:0)

使用Microsoft SQL,您可以将结果传递给DataTable,方法是将其传递给SQLDataAdapter实例。填充数据表后,可以将其作为数据源分配给DataGridView。这样做的好处是DataGridView可以自动更新其内容。

我没有使用SQL Lite的经验,但我希望它的API类似。

Dim table As New DataTable()
Dim dataAdapter As New SqlClient.SqlDataAdapter(cmd)

dataAdapter.Fill(table)
dataGridView.DataSource = table

答案 3 :(得分:0)

Function getData(ByVal sql As String) As DataTable

   OpenConnection()
   SQLcommand = SQLconnect.CreateCommand
   SQLcommand.CommandText = sql
   Dim dataAdapter As New SQLiteDataAdapter(SQLcommand)
   Dim table As New DataTable
   dataAdapter.Fill(table)
   CloseConnection()
   Return table       
End Function

On Main MyDataGrid.DataSource = getData(“select * from tb_employee”)

答案 4 :(得分:0)

这对我有用:

    Dim conn = New SQLiteConnection("Data Source=MyDataBaseName.sqlite;Version=3")

    Try
        Using (conn)
            conn.Open()

            Dim sql = "SELECT * FROM table"

            Dim cmdDataGrid As SQLiteCommand = New SQLiteCommand(sql, conn)

            Dim da As New SQLiteDataAdapter
            da.SelectCommand = cmdDataGrid
            Dim dt As New DataTable
            da.Fill(dt)
            DataGridView1.DataSource = dt

            Dim readerDataGrid As SQLiteDataReader = cmdDataGrid.ExecuteReader()

        End Using

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try