【问题标题】:How to read a SQLite database into a DataGridView object如何将 SQLite 数据库读入 DataGridView 对象
【发布时间】:2014-06-10 05:30:45
【问题描述】:

我正在使用 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

【问题讨论】:

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


    【解决方案1】:

    这对我有用:

        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
    

    【讨论】:

      【解决方案2】:
      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
      

      在主要 MyDataGrid.DataSource = getData("select * from tb_employee")

      【讨论】:

        【解决方案3】:
        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
        

        【讨论】:

          【解决方案4】:

          我在 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;
               }
           }
          

          【讨论】:

            【解决方案5】:

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

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

            Dim table As New DataTable()
            Dim dataAdapter As New SqlClient.SqlDataAdapter(cmd)
            
            dataAdapter.Fill(table)
            dataGridView.DataSource = table
            

            【讨论】:

              猜你喜欢
              • 2018-03-29
              • 2013-01-19
              • 1970-01-01
              • 2010-11-17
              • 2014-10-16
              • 2022-08-16
              • 2015-12-01
              • 2014-02-23
              • 1970-01-01
              相关资源
              最近更新 更多