Collections Generic Componentmodel Data Drawing Text Windows Forms Data Oledb Io
Collections Generic Componentmodel Data Drawing Text Windows Forms Data Oledb Io
Collections Generic Componentmodel Data Drawing Text Windows Forms Data Oledb 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)
{
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
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.
}