VB.net+SQLite 轻量级数据库连接

今天朋友有介绍Sqlite 轻量级数据库,发现用处蛮大的。

研究了下 蛮好用的,比调用excel之类的好用多了

下面介绍如何用把SQlite和VB.net连接起来查询

我用的是VS2005

1.引用System.Data.SQLite 如果是VS2005那System.Data.SQLite得版本要选择对应的支持Fwork 2.0的

2,写代码

  我把链接数据库改成了类似SQLHelper的类,方便以后引用如Button2的查询

Button1的查询就是基本的链接方法

SQLhelper在我的资源里有,大家可以下载

Imports System
Imports System.Data.SQLite
Imports System.Data

Public Class Form1
    ' Dim constr As String = "data source= E:\SQLite\mydatabase.sqlite"
    Dim str As String = Application.StartupPath & "\mydatabase.sqlite"
    Dim constr As String = "data source= " & str & ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SQLconn As New Data.SQLite.SQLiteConnection '定义数据库链接
        Dim sqlcmd As New SQLite.SQLiteCommand '定义查询操作
        Dim ds As New DataSet
        Dim salda As New SQLite.SQLiteDataAdapter
        Try
            SQLconn.ConnectionString = constr '链接数据库
            SQLconn.Open()
            sqlcmd.Connection = SQLconn
            sqlcmd.CommandText = "select * from t_emp"
            Dim sqlreader As SQLite.SQLiteDataReader = sqlcmd.ExecuteReader
            salda = New SQLite.SQLiteDataAdapter(sqlcmd.CommandText, SQLconn)

            salda.Fill(ds, 0)
            DGV1.DataSource = ds.Tables(0)


        Finally
            If Not (SQLconn Is Nothing) Then SQLconn.Dispose()
            SQLconn = Nothing
            If Not (salda Is Nothing) Then salda.Dispose()
            salda = Nothing
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sql As String = "select * from t_emp"
        Dim ds As DataSet
        ds = SQLiteHelper.ExecuteDataset(constr, CommandType.Text, sql)
        DGV1.DataSource = ds.Tables(0)
    End Sub
End Class



  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
VB.NET是一种面向对象的编程语言,可以轻松地操作各种类型的数据库,其中包括SQLite数据库SQLite是一种开源的关系型数据库,它非常流行,因为它是轻巧且易于使用。 在VB.NET中,可以使用SQLite.NET提供的SQLiteDataAdapter和SQLiteCommand对象来连接和操作SQLite数据库。具体步骤如下: 1.打开VB.NET开发环境,创建一个新的VB.NET项目; 2.在“工具”菜单下选择“NuGet包管理器”,在弹出的窗口中搜索“System.Data.SQLite”,并下载并安装该依赖项; 3.在VB.NET项目中添加一个引用“System.Data.SQLite”,并导入命名空间“System.Data.SQLite”; 4.创建一个SQLiteConnection对象,将连接字符串传递给它,以便连接到SQLite数据库; Dim connection As New SQLiteConnection("Data Source=pathToDatabase.db") 5.创建一个SQLiteCommand对象,可用于执行SQL命令; Dim command As New SQLiteCommand("SELECT * FROM tablename", connection) 6.使用DataAdapter对象读取和写入数据。可以使用DataAdapter的Fill方法填充DataTable(或DataSet),也可以使用DataAdapter的Update方法将更改保存回数据库。 Dim adapter As New SQLiteDataAdapter(command) Dim dataTable As New DataTable() adapter.Fill(dataTable) 7.关闭数据库连接。 connection.Close() 通过这些步骤,您可以轻松地使用VB.NET操作SQLite数据库。以SQLite为基础的应用程序可以以高效、可靠的方式存储数据,同时保持数据格式的完整性和一致性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cheug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值