-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerCRUD.vb
More file actions
133 lines (121 loc) · 6.59 KB
/
Copy pathCustomerCRUD.vb
File metadata and controls
133 lines (121 loc) · 6.59 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Imports MySql.Data.MySqlClient
Module CustomerCRUD
Public Sub AddCustomer(cus_name As String, cus_phone As String, cus_email As String, cus_address As String, priceCategory As String)
Using conn As MySqlConnection = GetConnection()
Dim query As String = "INSERT INTO customers (cus_name, cus_phone, cus_email, cus_address, price_category)
VALUES (@cus_name, @cus_phone, @cus_email, @cus_address, @cat)"
Try
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@cus_name", cus_name)
cmd.Parameters.AddWithValue("@cus_phone", cus_phone)
cmd.Parameters.AddWithValue("@cus_email", If(String.IsNullOrWhiteSpace(cus_email), DBNull.Value, cus_email))
cmd.Parameters.AddWithValue("@cus_address", cus_address)
cmd.Parameters.AddWithValue("@cat", priceCategory)
cmd.ExecuteNonQuery()
End Using
Catch ex As MySqlException When ex.Message.Contains("Unknown column")
' Legacy schema only: price_category column missing -> fall back to cus_pricetyre.
' Any other error (e.g. duplicate phone) propagates instead of silently retrying.
query = "INSERT INTO customers (cus_name, cus_phone, cus_email, cus_address, cus_pricetyre) VALUES (@cus_name, @cus_phone, @cus_email, @cus_address, @cat)"
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@cus_name", cus_name)
cmd.Parameters.AddWithValue("@cus_phone", cus_phone)
cmd.Parameters.AddWithValue("@cus_email", cus_email)
cmd.Parameters.AddWithValue("@cus_address", cus_address)
cmd.Parameters.AddWithValue("@cat", priceCategory)
cmd.ExecuteNonQuery()
End Using
End Try
End Using
End Sub
Public Function LoadCustomers() As DataTable
Dim dt As New DataTable()
Using conn As MySqlConnection = GetConnection()
' conn.Open()
Dim query As String = "SELECT * FROM customers"
Using da As New MySqlDataAdapter(query, conn)
da.Fill(dt)
End Using
End Using
Return dt
End Function
Public Sub UpdateCustomer(cus_id As Integer, cus_name As String, cus_phone As String, cus_email As String, cus_address As String, priceCategory As String)
Using conn As MySqlConnection = GetConnection()
Dim query As String = "UPDATE customers SET cus_name=@cus_name, cus_phone=@cus_phone, cus_email=@cus_email, cus_address=@cus_address, price_category=@cat WHERE cus_id=@cus_id"
Try
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@cus_id", cus_id)
cmd.Parameters.AddWithValue("@cus_name", cus_name)
cmd.Parameters.AddWithValue("@cus_phone", cus_phone)
cmd.Parameters.AddWithValue("@cus_email", If(String.IsNullOrWhiteSpace(cus_email), DBNull.Value, cus_email))
cmd.Parameters.AddWithValue("@cus_address", cus_address)
cmd.Parameters.AddWithValue("@cat", priceCategory)
cmd.ExecuteNonQuery()
End Using
Catch ex As MySqlException When ex.Message.Contains("Unknown column")
' Legacy schema only: price_category column missing -> fall back to cus_pricetyre.
' Any other error (e.g. duplicate phone) propagates instead of silently retrying.
query = "UPDATE customers SET cus_name=@cus_name, cus_phone=@cus_phone, cus_email=@cus_email, cus_address=@cus_address, cus_pricetyre=@cat WHERE cus_id=@cus_id"
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@cus_id", cus_id)
cmd.Parameters.AddWithValue("@cus_name", cus_name)
cmd.Parameters.AddWithValue("@cus_phone", cus_phone)
cmd.Parameters.AddWithValue("@cus_email", cus_email)
cmd.Parameters.AddWithValue("@cus_address", cus_address)
cmd.Parameters.AddWithValue("@cat", priceCategory)
cmd.ExecuteNonQuery()
End Using
End Try
End Using
End Sub
Public Sub DeleteCustomer(cus_id As Integer)
Using conn As MySqlConnection = GetConnection()
' conn.Open()
Dim query As String = "DELETE FROM customers WHERE cus_id=@cus_id"
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@cus_id", cus_id)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
''' <summary>Activate or deactivate a customer (soft enable/disable).</summary>
Public Sub SetCustomerActive(cus_id As Integer, active As Boolean)
Using conn As MySqlConnection = GetConnection()
Using cmd As New MySqlCommand("UPDATE customers SET is_active=@a WHERE cus_id=@cus_id", conn)
cmd.Parameters.AddWithValue("@a", If(active, 1, 0))
cmd.Parameters.AddWithValue("@cus_id", cus_id)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Public Function SearchCustomers(keyword As String) As DataTable
Dim dt As New DataTable()
Using conn As MySqlConnection = GetConnection()
Try
FillCustomerSearch(conn, dt, keyword, "price_category")
Catch ex As MySqlException
If ex.Message.Contains("Unknown column") Then
dt.Clear()
FillCustomerSearch(conn, dt, keyword, "cus_pricetyre")
Else
Throw
End If
End Try
End Using
Return dt
End Function
Private Sub FillCustomerSearch(conn As MySqlConnection, dt As DataTable, keyword As String, priceColumn As String)
Dim query As String = "SELECT * FROM customers " &
"WHERE cus_name LIKE @keyword " &
"OR cus_phone LIKE @keyword " &
"OR cus_email LIKE @keyword " &
"OR cus_address LIKE @keyword " &
"OR " & priceColumn & " LIKE @keyword"
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@keyword", "%" & keyword & "%")
Using da As New MySqlDataAdapter(cmd)
da.Fill(dt)
End Using
End Using
End Sub
End Module