0% found this document useful (0 votes)
68 views4 pages

Collections Generic Componentmodel Data Drawing Text Windows Forms Data Oledb Io

The code defines a class called frmQLSV that manages student data. It contains methods to load student data from a database into textboxes and a datagrid, insert, update, delete student records from the database, and clear the form fields. The load event handler loads initial student data and populates form fields and datagrid. Buttons trigger methods to perform CRUD operations on the student table in the database. The code needs separate data loading and connection closing methods to properly handle database operations.

Uploaded by

Nyny Pham
Copyright
© Attribution Non-Commercial (BY-NC)
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)
68 views4 pages

Collections Generic Componentmodel Data Drawing Text Windows Forms Data Oledb Io

The code defines a class called frmQLSV that manages student data. It contains methods to load student data from a database into textboxes and a datagrid, insert, update, delete student records from the database, and clear the form fields. The load event handler loads initial student data and populates form fields and datagrid. Buttons trigger methods to perform CRUD operations on the student table in the database. The code needs separate data loading and connection closing methods to properly handle database operations.

Uploaded by

Nyny Pham
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 4

C# code:

Lựa chọn code | Ẩn/Hiện code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace KNCSDLMDI
{
    public partial class frmQLSV : Form
    {
       
        DataSet ds = new DataSet();
        OleDbConnection cnn = new OleDbConnection();
        OleDbCommand cmd = new OleDbCommand();
        string sql;
        public frmQLSV()
        {
            InitializeComponent();
            
        }

        private void frmQLSV_Load(object sender, EventArgs e)
        {

            //DataGridViewCheckBoxColumn col = new


DataGridViewCheckBoxColumn();
            //Gridview.Columns.Add(col);
            Ketnoi();
            //dua du lieu ra textbox
            sql = "Select * From sinhvien";
            OleDbCommand cmd = new OleDbCommand(sql, cnn);
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            txtmssv.Text= reader["Mssv"].ToString();
            txttensv.Text=reader["tensv"].ToString();
            txtml.Text = reader["malop"].ToString();
            txtmk.Text = reader["makhoa"].ToString();
            reader.Close();
            //dua du lieu vao luoi

            OleDbDataAdapter sda = new OleDbDataAdapter();


            sda.SelectCommand = cmd;
            sda.Fill(ds, "sinhvien");
            DataTable dtb = ds.Tables["sinhvien"];
            Gridview.DataSource = dtb;
            //dua du lieu vao Combobox
            foreach (DataRow dtr in dtb.Rows)
            {
                comboBox1.Items.Add(dtr["Mssv"]);
            }
            cnn.Close();
        }

        private void Gridview_CellMouseClick(object
sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            
            row = Gridview.Rows[e.RowIndex];
            txtmssv.Text = row.Cells[0].Value.ToString();
            txttensv.Text = row.Cells[1].Value.ToString();
            txtml.Text = row.Cells[2].Value.ToString();
            txtmk.Text = row.Cells[3].Value.ToString();
            
            //hien thi du lieu qua form 2
            //ThongTinSinhVien.frmthongtinsinhvien ttsv = new
ThongTinSinhVien.frmthongtinsinhvien();
        }
        public void Ketnoi()
        {
            cnn.ConnectionString = "Provider =
Microsoft.Jet.OLEDB.4.0 ;" + "Data Source =" +
                               
Application.StartupPath + @"\Quanlysinhvien.mdb;" +
                                "Jet OLEDB:Database Password =;" +
                                "User ID = admin";
            cnn.Open();
            
            cmd.Connection = cnn;

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs
e)
        {
            int index = comboBox1.SelectedIndex;
            if (index >=0)
            {
                sql = "Select * From sinhvien where
Mssv='"+comboBox1.Text+"'";
                OleDbDataAdapter sda = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                OleDbCommand cmd = new OleDbCommand(sql,cnn);
                sda.SelectCommand = cmd;
                sda.Fill(ds, "sinhvien");
                DataTable dtb = ds.Tables["sinhvien"];
                Gridview.DataSource = dtb;
                cnn.Close();
               
            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            Ketnoi();
            //txtmssv.Enabled = true;
            string sql = "";
            sql = "INSERT INTO SINHVIEN(MSSV,TenSV,MALOP,MAKHOA)
VALUES('" + txtmssv.Text + "','" + txttensv.Text + "','" + txtml.Text + "','" 
+ txtmk.Text  + "')";
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            cnn.Close();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            Ketnoi();
            string sql = "";
            sql = "delete from sinhvien where mssv='" + txtmssv.Text + "'";
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            cnn.Close();
            
            try
            {
                txtmssv.Clear();
                txttensv.Clear();
                txtml.Clear();
                txtmk.Clear();
                Gridview.ClearSelection();
            }
            catch { }
            MessageBox.Show("Xoá thành công !");
            
            
            
           
            
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            Ketnoi();
            if (txtmssv.Text != "")
            {
                string sql = ""; 
                sql = "UPDATE sinhvien SET tensv ='" + txttensv.Text + "',
malop ='" + txtml.Text + "',makhoa ='" + txtmk.Text + "' WHERE
mssv='" + txtmssv.Text +"'";
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                MessageBox.Show("Cập Nhật Thành Công");
                cnn.Close();
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập mã sinh viên ...", "Thông
báo");
            }

        }

        public void Load_DuLieu()
        {
            Ketnoi();
            
            sql = "Select * From sinhvien";
            OleDbCommand cmd = new OleDbCommand(sql, cnn);
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            txtmssv.Text = reader["Mssv"].ToString();
            txttensv.Text = reader["tensv"].ToString();
            txtml.Text = reader["malop"].ToString();
            txtmk.Text = reader["makhoa"].ToString();
            reader.Close();
            //dua du lieu vao luoi

            OleDbDataAdapter sda = new OleDbDataAdapter();


            sda.SelectCommand = cmd;
            sda.Fill(ds, "sinhvien");
            DataTable dtb = ds.Tables["sinhvien"];
            Gridview.DataSource = dtb;
            //dua du lieu vao Combobox
            foreach (DataRow dtr in dtb.Rows)
            {
                comboBox1.Items.Add(dtr["Mssv"]);
            }
            cnn.Close();
        }
        private void btnth_Click(object sender, EventArgs e)
        {
            txtmssv.Text = "";
            txttensv.Text = "";
            txtml.Text="";
            txtmk.Text = "";
            txtmssv.Select();
        }
       
    }
}

bạn tạo các Hàm load dữ liệu lên gird riêng biệt (ví dụ là loadDATA())
Khi viết code nút xóa, 
Try
{
code nút xóa
}
catch
{
}
finaly
{
dieConnection;()ham này để ngắt kết nối thôi
loadDATA();// Gọi lại hàm load DataGird ở mối thao tác.
}

You might also like