forked from mohany203/Spectra-Laboratory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionality.vb
105 lines (81 loc) · 3.97 KB
/
Functionality.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Imports System.Data.SqlClient
Module Functionality
' Connection string to connect to the SQL Server database
Private connectionString As String = "server = Legion-5\SQLEXPRESS ; database = lab; Integrated security = True"
' Function to execute a SELECT query
Public Function SelectQuery(query As String, parameters As List(Of SqlParameter)) As DataTable
Dim dt As New DataTable()
Using con As New SqlConnection(connectionString)
Using cmd As New SqlCommand(query, con)
If parameters IsNot Nothing Then
cmd.Parameters.AddRange(parameters.ToArray())
End If
Using da As New SqlDataAdapter(cmd)
da.Fill(dt)
End Using
End Using
End Using
Return dt
End Function
' Function to execute an INSERT query
Public Function InsertQuery(query As String, parameters As List(Of SqlParameter)) As Integer
Return ExecuteNonQuery(query, parameters)
End Function
' Function to execute an UPDATE query
Public Function UpdateQuery(query As String, parameters As List(Of SqlParameter)) As Integer
Return ExecuteNonQuery(query, parameters)
End Function
' Function to execute a DELETE query
Public Function DeleteQuery(query As String, parameters As List(Of SqlParameter)) As Integer
Return ExecuteNonQuery(query, parameters)
End Function
' Helper function to execute non-query commands (INSERT, UPDATE, DELETE)
Private Function ExecuteNonQuery(query As String, parameters As List(Of SqlParameter)) As Integer
Dim result As Integer
Using con As New SqlConnection(connectionString)
Using cmd As New SqlCommand(query, con)
If parameters IsNot Nothing Then
cmd.Parameters.AddRange(parameters.ToArray())
End If
con.Open()
result = cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Return result
End Function
' Function to build a parameterized SELECT query
Public Function BuildSelectQuery(table As String, columns As List(Of String), condition As String, parameters As List(Of SqlParameter)) As String
Dim columnList As String = String.Join(", ", columns)
Dim query As String = $"SELECT {columnList} FROM {table}"
If Not String.IsNullOrEmpty(condition) Then
query &= " WHERE " & condition
End If
Return query
End Function
' Function to build a parameterized INSERT query
Public Function BuildInsertQuery(table As String, columns As List(Of String), values As List(Of String)) As String
Dim columnList As String = String.Join(", ", columns)
Dim valuePlaceholders As String = String.Join(", ", values.Select(Function(v, i) "@param" & i))
Dim query As String = $"INSERT INTO {table} ({columnList}) VALUES ({valuePlaceholders})"
MessageBox.Show(query)
Return query
End Function
' Function to build a parameterized UPDATE query
Public Function BuildUpdateQuery(table As String, updates As List(Of String), condition As String, parameters As List(Of SqlParameter)) As String
Dim updateList As String = String.Join(", ", updates)
Dim query As String = $"UPDATE {table} SET {updateList}"
If Not String.IsNullOrEmpty(condition) Then
query &= " WHERE " & condition
End If
Return query
End Function
' Function to build a parameterized DELETE query
Public Function BuildDeleteQuery(table As String, condition As String, parameters As List(Of SqlParameter)) As String
Dim query As String = $"DELETE FROM {table}"
If Not String.IsNullOrEmpty(condition) Then
query &= " WHERE " & condition
End If
Return query
End Function
End Module