在项目开发中充分体会到了一个精简数据存储模块的重要性及实用性;在综合了三年开发经验的基础上,向各位推介一下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