- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
Cập nhật hình ảnh xuống SQL
Bài viết này mình sẽ giới thiệu các bạn cách lưu hình ảnh xuống database SQL như thế nào. Trước tiên bạn thiết kế data như sau
:
Bạn thiết form theo mẫu sau:
Ở bài viết này mình sẽ nhấn mạnh vào cách lưu hình ảnh xuống SQL như thế nào.
- Trước tiên bạn kéo công cụ OpenFileDialog để mở các file hình ảnh. Tại sự kiện Double_Click của PictureBox ta viết sau:
Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
OpenFileDialog1.Filter = "Select Images |*.jpg||*.png" 'Lọc lấy file hình ảnh có phần mở rộng *.jpg, *.png
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Load(OpenFileDialog1.FileName)
End If
End Sub
Sau khi chọn hình xong và nó load lên đối tượng PictureBox, bước tiếp theo ta lưu hình này xuống data viết thủ tục sau:
Public Sub save_HinhNhanVien()
Dim mshinh As New MemoryStream
PictureBox1.Image.Save(mshinh, PictureBox1.Image.RawFormat)
Dim arrhinh() As Byte = mshinh.GetBuffer()
mshinh.Close()
Dim strsql As String = "update tbl_nhanvien set hinh=@hinh where manv=@manv"
CreateConnectSQL()
Dim cmd As New SqlCommand(strsql, strConnectString)
cmd.Parameters.Add(New SqlParameter("@hinh", arrhinh))
cmd.Parameters.Add(New SqlParameter("@manv", txtMaNV.Text))
cmd.ExecuteNonQuery()
strConnectString.Close()
End Sub
Tiếp đó tại sự kiện nút lưu ta viết như sau, ở đây mình không đề cập đến các câu lệnh update, save, delete.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If txtMaNV.Text.Length > 0 Then
If txtHoTen.Text.Length > 0 Then
Dim i As Integer = CheckID("select count(*) from tbl_nhanvien where manv='" & txtMaNV.Text & "'")
If i = 0 Then 'Kiểm tra nếu mã nhân viên trùng thì cập nhật
_Save("insert into tbl_nhanvien(manv,hoten,ngaysinh,noisinh,diachi) values ('" & txtMaNV.Text & "',N'" & txtHoTen.Text & "','" & txtNgaySinh.Text & "',N'" & txtNoiSinh.Text & "',N'" & txtDiaChi.Text & "')")
Else
_Save("update tbl_nhanvien set hoten=N'" & txtHoTen.Text & "',ngaysinh='" & txtNgaySinh.Text & "',noisinh=N'" & txtNoiSinh.Text & "',diachi=N'" & txtDiaChi.Text & "' where manv='" & txtMaNV.Text & "'")
End If
Try
save_HinhNhanVien() 'Cập nhật hình ảnh
Catch ex As Exception
MessageBox.Show(ex.Message, "Image")
End Try
ClearText()
loadData()
Else
MessageBox.Show("Tên nhân viên không được bỏ trống")
txtHoTen.Focus()
End If
Else
MessageBox.Show("Mã nhân viên không được bỏ trống")
txtMaNV.Focus()
End If
End Sub
- Bước tiếp theo load hình ảnh lên form, tại sự kiện TextChanged của txt_MaNV ta viết như sau:
Private Sub txtMaNV_TextChanged(sender As Object, e As EventArgs) Handles txtMaNV.TextChanged
Try
Dim arrhinh() As Byte = CType(load_HinhNhanVien(txtMaNV.Text).Tables(0).Rows(0)("hinh"), Byte())
Dim mshinh As New MemoryStream(arrhinh)
PictureBox1.Image = Image.FromStream(mshinh)
Catch ex As Exception
' MessageBox.Show(ex.Message, "Image", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
- Xong rồi đấy, nhấn F5 để thưởng thức kết quả nao :v