In project development, I fully realized the importance and practicability of a simplified data storage module. Based on three years of development experience, I would like to introduce the sqlite database to you, we hope more programmers can support the open source spirit.
Imports System. data. SQLite
Public Class Form1
Dim conn As SQLiteConnection
Private Sub button#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 ("opened successfully! ")
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 ("successful ")
Else
MsgBox ("failed ")
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 ("inserted successfully ")
End If
SelectShowInfo ()
'
Cmd = conn. CreateCommand ()
Cmd. CommandText = "update Test set TestName = 'dynamic and static 1 '"
Result = cmd. ExecuteNonQuery ()
If result <> 0 Then
MsgBox ("updated successfully ")
End If www.2cto.com
SelectShowInfo ()
'
Cmd = conn. CreateCommand ()
Cmd. CommandText = "delete from Test"
Result = cmd. ExecuteNonQuery ()
If result <> 0 Then
MsgBox ("deleted successfully ")
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
Author: angxiao