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

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部