0% found this document useful (0 votes)
4 views5 pages

codeform3vjp1

The document is a VB.NET class for managing customer data in a SQL database, including methods to add, edit, delete, and search for customers. It establishes a connection to the database using a connection string and handles exceptions during database operations. The class also includes a method to reset the form fields for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

codeform3vjp1

The document is a VB.NET class for managing customer data in a SQL database, including methods to add, edit, delete, and search for customers. It establishes a connection to the database using a connection string and handles exceptions during database operations. The class also includes a method to reset the form fields for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Imports System.Data.

SqlClient

Public Class FormCustomer


Dim connectionString As String = "Data Source=YourServerName;Initial
Catalog=YourDatabaseName;Integrated Security=True"
Dim connection As New SqlConnection(connectionString)

' Hàm mở kết nối


Private Sub OpenConnection()
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
End Sub

' Hàm đóng kết nối


Private Sub CloseConnection()
If connection.State = ConnectionState.Open Then
connection.Close()
End If
End Sub

' Thêm khách hàng


Private Sub AddCustomer(name As String, address As String, phone As
String)
Try
OpenConnection()
Dim query As String = "INSERT INTO Customers (Name, Address,
Phone) VALUES (@Name, @Address, @Phone)"
Dim command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@Name", name)
command.Parameters.AddWithValue("@Address", address)
command.Parameters.AddWithValue("@Phone", phone)
command.ExecuteNonQuery()
MessageBox.Show("Thêm khách hàng thành công!")
Catch ex As Exception
MessageBox.Show("Lỗi: " & ex.Message)
Finally
CloseConnection()
End Try
End Sub

' Sửa thông tin khách hàng


Private Sub EditCustomer(id As Integer, name As String, address As String,
phone As String)
Try
OpenConnection()
Dim query As String = "UPDATE Customers SET Name = @Name,
Address = @Address, Phone = @Phone WHERE CustomerID = @ID"
Dim command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@ID", id)
command.Parameters.AddWithValue("@Name", name)
command.Parameters.AddWithValue("@Address", address)
command.Parameters.AddWithValue("@Phone", phone)
command.ExecuteNonQuery()
MessageBox.Show("Sửa thông tin khách hàng thành công!")
Catch ex As Exception
MessageBox.Show("Lỗi: " & ex.Message)
Finally
CloseConnection()
End Try
End Sub

' Xóa khách hàng


Private Sub DeleteCustomer(id As Integer)
Try
OpenConnection()
Dim query As String = "DELETE FROM Customers WHERE
CustomerID = @ID"
Dim command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@ID", id)
command.ExecuteNonQuery()
MessageBox.Show("Xóa khách hàng thành công!")
Catch ex As Exception
MessageBox.Show("Lỗi: " & ex.Message)
Finally
CloseConnection()
End Try
End Sub

' Tìm kiếm khách hàng theo tên


Private Sub SearchCustomerByName(name As String)
Try
OpenConnection()
Dim query As String = "SELECT * FROM Customers WHERE Name
LIKE '%' + @Name + '%'"
Dim command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@Name", name)
Dim reader As SqlDataReader = command.ExecuteReader()
' Hiển thị kết quả tìm kiếm
If reader.HasRows Then
While reader.Read()
' Giả sử bạn có các TextBox để hiển thị thông tin khách hàng
TextBoxID.Text = reader("CustomerID").ToString()
TextBoxName.Text = reader("Name").ToString()
TextBoxAddress.Text = reader("Address").ToString()
TextBoxPhone.Text = reader("Phone").ToString()
End While
Else
MessageBox.Show("Không tìm thấy khách hàng!")
End If
reader.Close()
Catch ex As Exception
MessageBox.Show("Lỗi: " & ex.Message)
Finally
CloseConnection()
End Try
End Sub

' Quay lại: Ví dụ quay lại trạng thái ban đầu của form
Private Sub ResetForm()
TextBoxID.Clear()
TextBoxName.Clear()
TextBoxAddress.Clear()
TextBoxPhone.Clear()
MessageBox.Show("Form đã được reset!")
End Sub
' Các sự kiện button trên form (giả sử bạn có các button tương ứng trong
form)
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles
btnAdd.Click
AddCustomer(TextBoxName.Text, TextBoxAddress.Text,
TextBoxPhone.Text)
End Sub

Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles


btnEdit.Click
EditCustomer(Convert.ToInt32(TextBoxID.Text), TextBoxName.Text,
TextBoxAddress.Text, TextBoxPhone.Text)
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles


btnDelete.Click
DeleteCustomer(Convert.ToInt32(TextBoxID.Text))
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles


btnSearch.Click
SearchCustomerByName(TextBoxName.Text)
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles


btnReset.Click
ResetForm()
End Sub
End Class

You might also like