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

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/vb/259639.html

猜你在找的VB相关文章