0% found this document useful (0 votes)
5 views11 pages

Bai Tap Thuc Hanh Thuc Tap SQL

The document outlines the implementation of a SQL database connection class and several forms for managing product categories, items, and employees in a Windows Forms application. It includes methods for displaying, adding, updating, and deleting records in the database, as well as handling user input and interactions with DataGridViews. The code is written in VB.NET and utilizes ADO.NET for database operations.

Uploaded by

Huy Đào Công
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Bai Tap Thuc Hanh Thuc Tap SQL

The document outlines the implementation of a SQL database connection class and several forms for managing product categories, items, and employees in a Windows Forms application. It includes methods for displaying, adding, updating, and deleting records in the database, as well as handling user input and interactions with DataGridViews. The code is written in VB.NET and utilizes ADO.NET for database operations.

Uploaded by

Huy Đào Công
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Tạo class kết nối vào csdl SQL và hàm đọc, thủ tục ghi dữ
liệu:
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient

Public Class XuLyDuLieu


Dim strCnn As String = "Data Source = .;Initial Catalog =
VLXD;Integrated Security=true;"
'Dim strCnn As String = "Data Source = .;Initial Catalog =
VLXD;persist security info = true; User Id=sa;
Password=281285; Connect Timeout =50"

Dim cnn As New SqlConnection(strCnn)


Dim dt As New DataTable()
Function Doc(ByVal strSQL As String)
Dim da As New SqlDataAdapter(strSQL, cnn)
dt.Clear()
da.Fill(dt)
Return dt
End Function

Sub ThucHienCauLenh(ByVal strSQL As String)


Try
If (cnn.State = ConnectionState.Closed) Then
cnn.Open()
End If
Dim cmd As New SqlCommand(strSQL, cnn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Có lỗi xẩy ra")
End Try
End Sub
End Class

2. Tạo form cập nhật danh mục Loại Hàng:


Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class frmLoaiHang
Dim objXuLyDuLieu As New XuLyDuLieu
Public MaLoai As Integer
Sub HienThiDuLieu()
DataGridView1.DataSource = objXuLyDuLieu.Doc("select *
from LoaiHangHoa")
End Sub
Function KiemTraDuLieu()
If txtTenLoaiHang.Text = "" Then
MsgBox("Bạn chưa nhập tên loại hàng")
Return False
Else
Return True
End If
End Function

Private Sub frmLoaiHang_Load(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
HienThiDuLieu()
End Sub

Private Sub btnThem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThem.Click
If KiemTraDuLieu() Then
objXuLyDuLieu.ThucHienCauLenh("Insert into
LoaiHangHoa (TenLoai) values (N'" + txtTenLoaiHang.Text +
"')")
HienThiDuLieu()
End If
End Sub

Private Sub btnSua_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnSua.Click
If KiemTraDuLieu() Then
objXuLyDuLieu.ThucHienCauLenh("Update LoaiHangHoa
set TenLoai=N'" + txtTenLoaiHang.Text + "' where MaLoai=" +
MaLoai.ToString())
HienThiDuLieu()
End If
End Sub

Private Sub btnXoa_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnXoa.Click
objXuLyDuLieu.ThucHienCauLenh("delete from LoaiHangHoa
where MaLoai=" & MaLoai.ToString())
HienThiDuLieu()
End Sub

Private Sub btnThoat_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThoat.Click
Close()
End Sub

Private Sub DataGridView1_MouseClick(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Handles DataGridView1.MouseClick
Try
Dim Dong As Integer =
DataGridView1.CurrentRow.Index
MaLoai = DataGridView1.Rows(Dong).Cells(0).Value
txtTenLoaiHang.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
Catch
End Try
End Sub

Private Sub DataGridView1_KeyDown(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles DataGridView1.KeyDown
Try
Dim Dong As Integer =
DataGridView1.CurrentRow.Index
If e.KeyCode = Keys.Up Then
Dong = DataGridView1.CurrentRow.Index - 1
End If
If e.KeyCode = Keys.Down Then
Dong = DataGridView1.CurrentRow.Index + 1
End If
MaLoai = DataGridView1.Rows(Dong).Cells(0).Value
txtTenLoaiHang.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
Catch ex As Exception
End Try
End Sub
End Class

3. Tạo form cập nhật danh mục mặt hàng


Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class frmMatHang
Dim objXuLyDuLieu As New XuLyDuLieu
Public MaHang As Integer

Sub HienThiDuLieu()
Try
Dim Str As String
Str = "select MaHang, TenLoai, TenHang, TenDVT from
HangHoa inner join LoaiHangHoa on HangHoa.MaLoai=LoaiHangHoa.MaLoai
left join DonViTinh on HangHoa.MaDVT=DonViTinh.MaDVT Where 1=1"
If (ckbTimTheoLoaiHang.Checked) Then
Str = Str + " AND HangHoa.MaLoai=" +
cmbTimTheoLoaiHang.SelectedValue.ToString()
End If
If ckbTimTheoTen.Checked Then
Str = Str + " AND TenHang like N'%" +
txtTimTheoTenHang.Text + "%'"
End If
DataGridView1.DataSource = objXuLyDuLieu.Doc(Str)
Catch ex As Exception
End Try
End Sub

Function KiemTraDuLieu()
If txtTenHang.Text = "" Then
Return False
Else
Return True
End If
End Function

Private Sub frmMatHang_Load(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim objXuLyDuLieu1 As New XuLyDuLieu
cmbLoaiHang.DataSource = objXuLyDuLieu1.Doc("Select
* from LoaiHangHoa order by TenLoai")
cmbLoaiHang.DisplayMember = "TenLoai"
cmbLoaiHang.ValueMember = "MaLoai"

Dim objXuLyDuLieu2 As New XuLyDuLieu


cmbDVT.DataSource = objXuLyDuLieu2.Doc("Select *
from DonViTinh order by TenDVT")
cmbDVT.DisplayMember = "TenDVT"
cmbDVT.ValueMember = "MaDVT"

Dim objXuLyDuLieu3 As New XuLyDuLieu


cmbTimTheoLoaiHang.DataSource =
objXuLyDuLieu3.Doc("Select * from LoaiHangHoa order by TenLoai")
cmbTimTheoLoaiHang.DisplayMember = "TenLoai"
cmbTimTheoLoaiHang.ValueMember = "MaLoai"

cmbLoaiHang.SelectedIndex = 0
cmbDVT.SelectedIndex = 0
cmbTimTheoLoaiHang.SelectedIndex = 0

HienThiDuLieu()
Catch ex As Exception
End Try

End Sub

Private Sub btnThem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThem.Click
If KiemTraDuLieu() Then
objXuLyDuLieu.ThucHienCauLenh("Insert into HangHoa
(MaLoai, TenHang, MaDVT) values (" +
cmbLoaiHang.SelectedValue.ToString() + ", N'" + txtTenHang.Text
+ "', " + cmbDVT.SelectedValue.ToString() + ")")
HienThiDuLieu()

End If
End Sub

Private Sub btnSua_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnSua.Click
If KiemTraDuLieu() Then
objXuLyDuLieu.ThucHienCauLenh("Update HangHoa set
MaLoai=" + cmbLoaiHang.SelectedValue.ToString() + ", TenHang=N'"
+ txtTenHang.Text + "', MaDVT= " +
cmbDVT.SelectedValue.ToString() + " where MaHang=" +
MaHang.ToString())
HienThiDuLieu()
End If
End Sub

Private Sub btnXoa_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnXoa.Click
objXuLyDuLieu.ThucHienCauLenh("delete from HangHoa where
MaHang=" & MaHang.ToString())
HienThiDuLieu()
End Sub

Private Sub btnThoat_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThoat.Click
Close()
End Sub

Private Sub DataGridView1_MouseClick(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Handles DataGridView1.MouseClick
Try
Dim Dong As Integer = DataGridView1.CurrentRow.Index
MaHang = DataGridView1.Rows(Dong).Cells(0).Value
cmbLoaiHang.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
txtTenHang.Text =
DataGridView1.Rows(Dong).Cells(2).Value.ToString()
cmbDVT.Text =
DataGridView1.Rows(Dong).Cells(3).Value.ToString()

Catch
End Try
End Sub

Private Sub DataGridView1_KeyDown(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles DataGridView1.KeyDown
Try
Dim Dong As Integer = DataGridView1.CurrentRow.Index
If e.KeyCode = Keys.Up Then
Dong = DataGridView1.CurrentRow.Index - 1
End If
If e.KeyCode = Keys.Down Then
Dong = DataGridView1.CurrentRow.Index + 1
End If
MaHang = DataGridView1.Rows(Dong).Cells(0).Value
cmbLoaiHang.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
txtTenHang.Text =
DataGridView1.Rows(Dong).Cells(2).Value.ToString()
cmbDVT.Text =
DataGridView1.Rows(Dong).Cells(3).Value.ToString()

Catch ex As Exception
End Try
End Sub

Private Sub btnTim_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnTim.Click
HienThiDuLieu()
End Sub
End Class

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.IO
Public Class frmNhanVien
Dim objXuLyDuLieu As New XuLyDuLieu
Public MaNV As String
Sub HienThiDuLieu()
DataGridView1.DataSource = objXuLyDuLieu.Doc("select *
from NhanVien")
End Sub
Function KiemTraDuLieu()
If txtHoTen.Text = "" Then
MsgBox("Bạn chưa nhập tên nv")
Return False
Else
Return True
End If
End Function
Private Sub frmNhanVien_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
HienThiDuLieu()
End Sub

Private Sub btnThem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThem.Click
If KiemTraDuLieu() Then
Dim str As String = "Insert into NhanVien (TenNV,
NgaySinh, SoCMND, Tel) values (N'" + txtHoTen.Text + "', '" +
dtpNgaySinh.Value.ToString("MM/dd/yyyy") + "', '" +
txtSoCMND.Text + "', N'" + txtTel.Text + "')"
objXuLyDuLieu.ThucHienCauLenh("Insert into
NhanVien (HoTen, NgaySinh, SoCMND, Tel) values (N'" +
txtHoTen.Text + "', '" +
dtpNgaySinh.Value.ToString("MM/dd/yyyy") + "', '" +
txtSoCMND.Text + "', N'" + txtTel.Text + "')")
HienThiDuLieu()
End If
End Sub

Private Sub btnSua_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnSua.Click
If KiemTraDuLieu() Then
objXuLyDuLieu.ThucHienCauLenh("Update NhanVien set
HoTen=N'" + txtHoTen.Text + "', NgaySinh=N'" +
dtpNgaySinh.Value.ToString("MM/dd/yyyy") + "', SoCMND='" +
txtSoCMND.Text + "', Tel=N'" + txtTel.Text + "' where MaNV=" +
MaNV.ToString())

Try
Dim mstr As New MemoryStream()
PictureBox1.Image.Save(mstr,
PictureBox1.Image.RawFormat)

Dim arrImage As Byte() = mstr.GetBuffer()


'Chuổi câu lệnh để insert image vào database, với
tham số @Pic
Dim cmd As String = "update NhanVien set Anh = @Pic
where MaNV=" + MaNV
'Tiếp theo chúng ta thực hiện Insert Image vào
database
Dim strCnn As String = "Data Source = .\
SQLEXPRESS;Initial Catalog = VLXD;Integrated Security=true;"
Dim con As New SqlConnection(strCnn) ' tạo đối
tượng kết nối mới
Dim comm As New SqlCommand(cmd, con)
comm.Parameters.Add(New SqlParameter("@Pic",
SqlDbType.Image)).Value = arrImage
Try
If (con.State = ConnectionState.Closed) Then
con.Open()
End If
comm.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Có lỗi xẩy ra")
End Try
Catch ex As Exception
End Try
HienThiDuLieu()
End If
End Sub

Private Sub btnXoa_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnXoa.Click
objXuLyDuLieu.ThucHienCauLenh("delete from NhanVien
where MaNV=" & MaNV)
HienThiDuLieu()
End Sub

Private Sub btnThoat_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnThoat.Click
Close()
End Sub

Private Sub DataGridView1_MouseClick(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Handles DataGridView1.MouseClick
Try
Dim Dong As Integer =
DataGridView1.CurrentRow.Index
MaNV =
DataGridView1.Rows(Dong).Cells(0).Value.ToString()
txtHoTen.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
dtpNgaySinh.Value =
DataGridView1.Rows(Dong).Cells(2).Value
txtSoCMND.Text =
DataGridView1.Rows(Dong).Cells(3).Value.ToString()
txtTel.Text =
DataGridView1.Rows(Dong).Cells(4).Value.ToString()

Dim strCnn As String = "Data Source = .\


SQLEXPRESS;Initial Catalog = VLXD;Integrated Security=true;"
Dim cmd As String = "SELECT Anh FROM NhanVien
where MaNV=" + MaNV.ToString()
Dim con As New SqlConnection(strCnn) ' tạo đối
tượng kết nối mới
Dim comm As New SqlCommand(cmd, con)
con.Open()
Dim b As Byte() = comm.ExecuteScalar() ' đọc dữ
liệu kiểu byte
Dim mem As New MemoryStream(b)
PictureBox1.Image = Image.FromStream(mem)
Catch
End Try
End Sub

Private Sub DataGridView1_KeyDown(ByVal sender As


System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles DataGridView1.KeyDown
Try
Dim Dong As Integer =
DataGridView1.CurrentRow.Index
If e.KeyCode = Keys.Up Then
Dong = DataGridView1.CurrentRow.Index - 1
End If
If e.KeyCode = Keys.Down Then
Dong = DataGridView1.CurrentRow.Index + 1
End If
MaNV =
DataGridView1.Rows(Dong).Cells(0).Value.ToString()
txtHoTen.Text =
DataGridView1.Rows(Dong).Cells(1).Value.ToString()
dtpNgaySinh.Value =
DataGridView1.Rows(Dong).Cells(2).Value
txtSoCMND.Text =
DataGridView1.Rows(Dong).Cells(3).Value.ToString()
txtTel.Text =
DataGridView1.Rows(Dong).Cells(4).Value.ToString()
Dim strCnn As String = "Data Source = .\
SQLEXPRESS;Initial Catalog = VLXD;Integrated Security=true;"
Dim cmd As String = "SELECT Anh FROM NhanVien
where MaNV=" + MaNV.ToString()
Dim con As New SqlConnection(strCnn) ' tạo đối
tượng kết nối mới
Dim comm As New SqlCommand(cmd, con)
con.Open()
Dim b As Byte() = comm.ExecuteScalar() ' đọc dữ
liệu kiểu byte
Dim mem As New MemoryStream(b)
PictureBox1.Image = Image.FromStream(mem)

Catch ex As Exception
End Try
End Sub

Private Sub txtTel_Leave(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles txtTel.Leave
btnThem.Focus()
End Sub

Private Sub btnChenAnh_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
btnChenAnh.Click
Dim dlg As New OpenFileDialog
dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg|Bitmaps|
*.bmp|GIFs|*.gi f|JPEGs|*.jpg"
If (dlg.ShowDialog() = DialogResult.OK) Then
PictureBox1.Image = New Bitmap(dlg.FileName)
Dim name As String =
dlg.FileName.Substring(dlg.FileName.LastIndexOf("\") + 1,
dlg.FileName.Length - dlg.FileName.LastIndexOf("\") - 1)
End If
End Sub
End Class

You might also like