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 SystemImports System.Data.SQLiteImports System.DataPublic Class Form1    ' Dim constr As String = "data source= E:SQLitemydatabase.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 SubEnd Class

推荐阅读