Vb.net uses system. data. sqlite. dll to implement the sqlite database programming example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.