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

How to read a SQLite database into a DataGridView object(如何将 SQLite 数据库读入 DataGridView 对象)
本文介绍了如何将 SQLite 数据库读入 DataGridView 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VS 2013 编写 VB 程序.我正在使用 SQLite.org 中 System.Data.SqLite.dll 中的方法.我可以很好地将我的数据库读入 ListBox 对象.我正在发布我为此使用的代码.我想做的是将此数据发送到 DataGridView 对象.我没能正确地做到这一点.

I am writing a VB program using VS 2013. I am using the methods in System.Data.SqLite.dll from SQLite.org. I can read my database fine into a ListBox object. I am posting my code that I am using for this. What I would like to do is send this data to a DataGridView object. I am having no luck doing it correctly.

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

推荐答案

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

I found a few similar questions on StackOverflow, but not close enough to post. Sorry to send you to another website. http://cplus.about.com/od/howtodothingsinc/ss/How-To-Use-Sqlite-From-Csharp_2.htm This is a copy/paste from the above link. The answer is to use the SQLLiteConnection and SQLLiteDataAdapter. Below is in C#, but easily convertable to 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;
     }
 }

这篇关于如何将 SQLite 数据库读入 DataGridView 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)
Android: Why is Room so slow?(Android:为什么Room这么慢?)
Remote Procedure call failed with sql server 2008 R2(使用 sql server 2008 R2 的远程过程调用失败)