You are on page 1of 5

'code trn form Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Windows.Forms Imports System.Data.SqlClient.

SqlException Public Class Form1 Dim cls As New ClsConnect #Region "Load d liu ln datagridview c tn HOCSINH" Private Sub bthienthi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buthienthi.Click cls.ShowDataToGrid("Select * from HOCSINH", DataGridView1) End Sub #End Region #Region "Lay du lieu tren datagrid dien vao cac textbox" Private Sub LoadTextBox() txtmahs.DataBindings.Clear() txtmahs.DataBindings.Add("Text", DataGridView1.DataSource, "MaHS") txthohs.DataBindings.Clear() txthohs.DataBindings.Add("Text", DataGridView1.DataSource, "HoHS") txttenhs.DataBindings.Clear() txttenhs.DataBindings.Add("Text", DataGridView1.DataSource, "TenHS") txtns.DataBindings.Clear() txtns.DataBindings.Add("Text", DataGridView1.DataSource, "NgaySinh") txtgt.DataBindings.Clear() txtgt.DataBindings.Add("Text", DataGridView1.DataSource, "GioiTinh") txtqq.DataBindings.Clear() txtqq.DataBindings.Add("Text", DataGridView1.DataSource, "QueQuan") txtmalop.DataBindings.Clear() txtmalop.DataBindings.Add("Text", DataGridView1.DataSource, "MaLop") txtmamh.DataBindings.Clear() txtmamh.DataBindings.Add("Text", DataGridView1.DataSource, "MaMH") txtmagv.DataBindings.Clear() txtmagv.DataBindings.Add("Text", DataGridView1.DataSource, "MaGV") End Sub #End Region #Region "Khi click chuot tren datagrid" Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click LoadTextBox() End Sub #End Region #Region " Xoa gia tri cua cac textbox tren form" Private Sub butlamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butlamlai.Click txtmahs.Text = "" txtmahs.Focus() txthohs.Text = "" txttenhs.Text = "" txtns.Text = "" txtgt.Text = "" txtqq.Text = ""

txtmalop.Text = "" txtmamh.Text = "" txtmagv.Text = "" End Sub #End Region #Region "Xa mt bn ghi" Private Sub butxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butxoa.Click If (txtmahs.Text.Trim().ToUpper() <> "") Then cls.Update_Data("Delete from HOCSINH where MaHS='" & txtmahs.Text.Trim().ToUpper() & "'") Else MessageBox.Show("Chon ban ghi can xoa tren luoi.", "Thong bao") End If cls.ShowDataToGrid("Select * from HOCSINH", DataGridView1) End Sub #End Region #Region "Thot" Private Sub butthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butthoat.Click If MsgBox("Bn c mun thot khi chng trnh khng ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "THOT") = MsgBoxResult.Yes Then End End If End Sub #End Region #Region "sua lai" Private Sub butsualai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butsualai.Click Dim lenh As String Dim st As String st = "Data Source=ARI;uid=sa;pwd=123456;Database=HOCSINH;Integrated Security=true;" Dim cn As New SqlConnection cn.ConnectionString = st If txtmahs.Text = "" Or txthohs.Text = "" Or txttenhs.Text = "" Or txtns.Text = "" Or txtgt.Text = "" Or txtqq.Text = "" Or txtmalop.Text = "" Or txtmamh.Text = "" Or txtmagv.Text = "" Then MsgBox("Bn phi nhp gi tr cn sa !!! ") Else lenh = "Update HOCSINH set HoHS = '" & txthohs.Text & "',TenHS = '" & txttenhs.Text & "',NgaySinh= '" & txtns.Text & "',GioiTinh = '" & txtgt.Text & "',QueQuan = '" & txtqq.Text & "',MaLop='" & txtmalop.Text & "' , MaMH = '" & txtmamh.Text & "', MaGV= '" & txtmagv.Text & "' where MaHS = '" & Trim(txtmahs.Text) & "'" Dim tam As New SqlCommand(lenh, cn) cn.Open() tam.ExecuteNonQuery() cn.Close() cls.ShowDataToGrid("Select * from HOCSINH", DataGridView1) End If End Sub #End Region End Class

'Code ca lp ClsConnect (Lp kt ni) ===================================================== Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlClient.SqlException Public Class ClsConnect 'Khai bao cac bien toan cuc Public cn As SqlConnection Public cmd As SqlCommand Public adapter As SqlDataAdapter Public ds As DataSet #Region " Khi to cc bin" Public Sub Khoitao() Try cn = New SqlConnection cmd = New SqlCommand adapter = New SqlDataAdapter ds = New DataSet Catch ex As Exception cn.Dispose() cmd.Dispose() adapter.Dispose() ds.Dispose() End Try End Sub #End Region #Region "Thu hoi bo nho da cap cho cac doi tuong" Public Sub Dong() cn.Dispose() cmd.Dispose() adapter.Dispose() ds.Dispose() End Sub #End Region #Region "Mo ket noi" Public Sub EnableConnect() Try Dim st As String ' Chuoi ket noi theo dac quyen he dieu hanh 'st = "Data Source=(local);Database=Northwind;Integrated Security=true;" ' Chuoi ket noi theo dac quyen cua he quan tri SQL st = "Data Source=ARI;uid=sa;pwd=123456;Database=HOCSINH;Integrated Security=true;" Khoitao() cn.ConnectionString = st cn.Open() Catch ex As Exception Dong() End Try End Sub #End Region

#Region "Dong ket noi" Public Sub DisableConnect() cn.Close() Dong() End Sub #End Region #Region "tra ve doi tuong data set" Public Function ReturnDataSet(ByVal st As String) As DataSet EnableConnect() cmd.CommandText = st cmd.CommandType = CommandType.Text cmd.Connection = cn adapter = New SqlDataAdapter(cmd) adapter.Fill(ds) Return ds DisableConnect() End Function #End Region #Region "Hien thi du lieu len gridview" Public Sub ShowDataToGrid(ByVal strSQL As String, ByVal grd As DataGridView) grd.DataSource = ReturnDataSet(strSQL).Tables(0).DefaultView End Sub #End Region #Region "Thu tuc cap nhat du lieu voi tham so dau vao la mot cau lenh sql" Public Sub Update_Data(ByVal sql As String) EnableConnect() cmd.CommandText = sql cmd.CommandType = CommandType.Text cmd.Connection = cn cmd.ExecuteNonQuery() DisableConnect() End Sub #End Region End Class

You might also like