vb.net利用system.data.sqlite.dll实现sqlite数据库编程示例

在项目开发中充分体会到了一个精简数据存储模块的重要性及实用性;在综合了三年开发经验的基础上,向各位推介一下sqlite数据库,希望更多的程序猿支持开源精神。

Imports System.data.SQLite

Public Class Form1

    Dim conn As SQLiteConnection

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If System.IO.File.Exists("test.db3") = True Then

            System.IO.File.Delete("test.db3")

        End If

        SQLiteConnection.CreateFile("test.db3")

        conn = New SQLiteConnection("Data Source=test.db3;Pooling=true;FailIfMissing=false")

        If conn.State <> ConnectionState.Open Then

            conn.Open()

            MsgBox("打开成功!")

        End If


        Dim cmd As New SQLiteCommand

        cmd.Connection = conn

        cmd.CommandText = "CREATE TABLE Test (ID INTEGER PRIMARY KEY,TestName VARCHAR(500),TestTime DateTime,Operator VARCHAR(100))"

        Dim result As Integer = cmd.ExecuteNonQuery()

        If result = 0 Then

            MsgBox("成功")

        Else

            MsgBox("失败")

        End If


        cmd = conn.CreateCommand()

        cmd.CommandText = "insert into Test(TestName,TestTime,Operator)values(@Name,@TestTime,@Operator)"

        cmd.Parameters.Add("@Name", Data.DbType.String).Value = "动静"

        cmd.Parameters.Add("@TestTime", Data.DbType.DateTime).Value = Now()

        cmd.Parameters.Add("@Operator", Data.DbType.String).Value = "peer"

        result = cmd.ExecuteNonQuery()

        If result <> 0 Then

            MsgBox("插入成功")

        End If

        SelectShowInfo()


        '

        cmd = conn.CreateCommand()

        cmd.CommandText = "update  Test set TestName='动静1'"

        result = cmd.ExecuteNonQuery()

        If result <> 0 Then

            MsgBox("更新成功")

        End If

        SelectShowInfo()


        '


        cmd = conn.CreateCommand()

        cmd.CommandText = "delete from Test"

        result = cmd.ExecuteNonQuery()

        If result <> 0 Then

            MsgBox("删除成功")

        End If

        SelectShowInfo()


        cmd.Dispose()


        If conn.State = ConnectionState.Open Then

            conn.Close()

        End If

    End Sub


    Public Sub SelectShowInfo()

        Dim sa As New SQLiteDataAdapter("select * from Test", conn)

        Dim ds As New System.Data.DataSet

        sa.Fill(ds, "Test")

        Dim mytable As New System.Data.DataTable

        mytable = ds.Tables("Test")

        Me.DataGridView1.DataSource = mytable

        Me.DataGridView1.Refresh()

    End Sub

End Class

//---------------------------------------------------------------------------------------------------------------------
sqlite在.net下的应用对于时间类型的处理很特殊:
1.插入时
'用datatime.ToString("s");这种方法转换成 iso 8601标准字符串格式
                lstRealDataFile.Add(DateTime.Now.ToString("s"))
2.查询时:
 SqlQueryStr.Append(" and SaveTime >= '" & Me.DtpSTime.Value.Date.ToString("s") & "'")
            SqlQueryStr.Append("  and SaveTime<= '" & Me.DtpETime.Value.ToString("s") & "'")

你可能感兴趣的:(SQLite,vb.net)