File tree 5 files changed +149
-0
lines changed
Establishing a Database Connection With SQL in VB.NET
How to Issue a Select and Load the Data Into an Array
How to Issue an Insert to Put Data Into a Database
Setting Up Your Local SQL Server
5 files changed +149
-0
lines changed Original file line number Diff line number Diff line change
1
+ Imports System
2
+ Imports System.Data.SQLite
3
+
4
+ Module Program
5
+ Sub Main(args As String ())
6
+ Dim connectionString As String = "Data Source=mydatabase.db;Version=3;"
7
+ Try
8
+ Using conn As New SQLiteConnection(connectionString)
9
+ conn.Open()
10
+ Console.WriteLine( "Successfully connected to the database!" )
11
+ 'You can perform database operations here.
12
+ End Using
13
+ Catch ex As SQLiteException
14
+ Console.WriteLine( "Error: " & ex.Message)
15
+ End Try
16
+ End Sub
17
+ End Module
Original file line number Diff line number Diff line change
1
+ Imports System.Data.SQLite
2
+
3
+ Module Program
4
+
5
+ Private Const DatabaseFileName As String = "mydatabase.db"
6
+
7
+ Sub Main(args As String ())
8
+ LoadDataIntoArray()
9
+ End Sub
10
+
11
+ Sub LoadDataIntoArray()
12
+ ' Set the connection string
13
+ Dim connectionString As String = $"Data Source={DatabaseFileName};Version=3;"
14
+
15
+ ' Set up the SQL query
16
+ Dim query As String = "SELECT Name FROM Users"
17
+
18
+ ' Create a list to store names retrieved from the database
19
+ Dim names As New List( Of String )()
20
+
21
+ Using conn As New SQLiteConnection(connectionString)
22
+ conn.Open()
23
+
24
+ Using cmd As New SQLiteCommand(query, conn)
25
+
26
+ Using reader As SQLiteDataReader = cmd.ExecuteReader()
27
+
28
+ ' Add all the names from the database to the list
29
+ While reader.Read()
30
+ names.Add(reader( "Name" ).ToString())
31
+ End While
32
+
33
+ End Using
34
+
35
+ End Using
36
+ End Using
37
+
38
+ ' So far the 'names' list has been populated with usernames from the database.
39
+ ' You can convert this list to an array if you want:
40
+ Dim namesArray() As String = names.ToArray()
41
+
42
+ ' Print array content for testing purposes
43
+ For Each name In namesArray
44
+ Console.WriteLine(name)
45
+ Next
46
+ End Sub
47
+
48
+ End Module
Original file line number Diff line number Diff line change
1
+ Imports System.Data.SQLite
2
+
3
+ Module Program
4
+
5
+ Private Const DatabaseFileName As String = "mydatabase.db"
6
+
7
+ Sub Main(args As String ())
8
+ InsertData( "John Doe" , "USA" )
9
+ End Sub
10
+
11
+ Sub InsertData(userName As String , userCountry As String )
12
+ ' Set the connection string
13
+ Dim connectionString As String = $"Data Source={DatabaseFileName};Version=3;"
14
+
15
+ ' Set up parameterized SQL query
16
+ Dim query As String = "INSERT INTO Users (Name, Country) VALUES (@Name, @Country)"
17
+
18
+ Using conn As New SQLiteConnection(connectionString)
19
+ conn.Open()
20
+
21
+ Using cmd As New SQLiteCommand(query, conn)
22
+ ' Set parameters
23
+ cmd.Parameters.AddWithValue( "@Name" , userName)
24
+ cmd.Parameters.AddWithValue( "@Country" , userCountry)
25
+
26
+ ' Run the query
27
+ cmd.ExecuteNonQuery()
28
+ End Using
29
+ End Using
30
+
31
+ Console.WriteLine( $"User {userName} from {userCountry} added successfully!" )
32
+ End Sub
33
+
34
+ End Module
Original file line number Diff line number Diff line change
1
+ Imports System.Data.SQLite
2
+
3
+ Module DatabaseInitializer
4
+
5
+ Sub InitializeDb()
6
+ ' Designate a new database file named "mydatabase.db"
7
+ Dim connectionString As String = "Data Source=mydatabase.db;Version=3;"
8
+ Using conn As New SQLiteConnection(connectionString)
9
+ conn.Open()
10
+
11
+ ' Create Users table
12
+ Dim createTableQuery As String = "CREATE TABLE IF NOT EXISTS Users (UserID INTEGER PRIMARY KEY, Name TEXT, Country TEXT)"
13
+ Using cmd As New SQLiteCommand(createTableQuery, conn)
14
+ cmd.ExecuteNonQuery()
15
+ End Using
16
+
17
+ ' Insert initial users
18
+ Dim insertUsers As String = "INSERT INTO Users (Name, Country) VALUES (?, ?)"
19
+ Using cmd As New SQLiteCommand(insertUsers, conn)
20
+ cmd.Parameters.AddWithValue( "Name" , "Alice" )
21
+ cmd.Parameters.AddWithValue( "Country" , "USA" )
22
+ cmd.ExecuteNonQuery()
23
+
24
+ cmd.Parameters.Clear()
25
+
26
+ cmd.Parameters.AddWithValue( "Name" , "Bob" )
27
+ cmd.Parameters.AddWithValue( "Country" , "UK" )
28
+ cmd.ExecuteNonQuery()
29
+
30
+ cmd.Parameters.Clear()
31
+
32
+ cmd.Parameters.AddWithValue( "Name" , "Charlie" )
33
+ cmd.Parameters.AddWithValue( "Country" , "Canada" )
34
+ cmd.ExecuteNonQuery()
35
+
36
+ cmd.Parameters.Clear()
37
+ End Using
38
+
39
+ Console.WriteLine( "Database and initial data created successfully!" )
40
+ End Using
41
+ End Sub
42
+
43
+ End Module
Original file line number Diff line number Diff line change
1
+ Module Program
2
+
3
+ Sub Main(args As String ())
4
+ DatabaseInitializer.InitializeDb()
5
+ End Sub
6
+
7
+ End Module
You can’t perform that action at this time.
0 commit comments