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

CCCCCCCCCCCC CCCCCCCCCCCCCCCCCC

The document defines a class called KetNoiDuLieu that connects to a database and performs operations like reading, updating, inserting, and deleting data. It includes methods to open a connection, execute queries, and read/write data to and from a DataTable. The class is used to display and manipulate data in database tables through a Windows form interface.

Uploaded by

Dinhlawyer Huu
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)
70 views4 pages

CCCCCCCCCCCC CCCCCCCCCCCCCCCCCC

The document defines a class called KetNoiDuLieu that connects to a database and performs operations like reading, updating, inserting, and deleting data. It includes methods to open a connection, execute queries, and read/write data to and from a DataTable. The class is used to display and manipulate data in database tables through a Windows form interface.

Uploaded by

Dinhlawyer Huu
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

//////////// class Ket noi du lieu////////////////// using System.Data.

SqlClient; class KetNoiDuLieu { public SqlConnection ketnoi; public SqlDataAdapter bodocghi; public SqlCommand lenh; public SqlCommandBuilder capnhat; //ham khoi tao ket noi public KetNoiDuLieu() { ketnoi = new SqlConnection(); string chuoiketnoi = "Data Source=.;Integrated Security=true;Initial Catalog=QLHH"; ketnoi.ConnectionString = chuoiketnoi; } //doc du lieu (select) public DataTable DocDuLieu(string sql) { ketnoi.Open(); bodocghi = new SqlDataAdapter(sql, ketnoi); DataTable bangnhcc = new DataTable(); bodocghi.Fill(bangnhcc); ketnoi.Close(); return bangnhcc; } //cap nhat dl public void CapnhatDulieu(SqlDataAdapter bodoc, DataTable bang) { capnhat = new SqlCommandBuilder(bodoc); bodoc.Update(bang); } //thao tac dl (insert, update, delete) public void ThaotacDulieu(string sql) { ketnoi.Open(); lenh = new SqlCommand(sql, ketnoi); lenh.ExecuteNonQuery(); ketnoi.Close(); } /////////////////hien thi du lieu trong datagir view chinh/////////////// SqlConnection ketnoi = new SqlConnection(); SqlDataAdapter bodocghi; SqlCommandBuilder capnhat; DataTable banghh = new DataTable(); SqlCommand lenh; int donghh; public void KetNoiDuLieu() { string chuoikn = "Data Source=.;Integrated Security=true;Initial Catalog=QLHH"; ketnoi.ConnectionString = chuoikn;

} private void HienThiDL() { KetNoiDuLieu(); ketnoi.Open(); string sql = "select* from hanghoa"; bodocghi = new SqlDataAdapter(sql, ketnoi); bodocghi.Fill(banghh); ketnoi.Close(); dgvhanghoa.DataSource = banghh; } ////////////hien thi len combox////////////////// private void Hientenhang() { string sql = "select *from hanghoa"; banghh = ketnoi.DocDuLieu(sql); cbtenhang.DataSource = banghh; cbtenhang.DisplayMember = "tenhang"; cbtenhang.ValueMember = "id_hh"; } ////////////////////su kien celllick/////////////////////// private void dgvhanghoa_CellClick(object sender,DataGridViewCellEventArgs e) { donghh = e.RowIndex; if (donghh >= 0) { txttenhang.Text = banghh.Rows[donghh]["tenhang"].ToString(); txtsoluongton.Text = banghh.Rows[donghh]["soluongton"].ToString(); txtdongia.Text = banghh.Rows[donghh]["dongia"].ToString(); } } ////////////////su kien text change (bat so dien thoai)///// private void txtdienthoai_TextChanged(object sender, EventArgs e) { try { if (txtdienthoai.Text != "") { Int64 tam = Int64.Parse(txtdienthoai.Text); if (txtdienthoai.Text.Length > 11) { MessageBox.Show("S i n tho i t i a l 11 s ", "Thng bo"); Int64 tam1; tam1 = tam / 10; txtdienthoai.Text = tam1.ToString(); txtdienthoai.Focus(); }

} } catch (Exception) { MessageBox.Show("Xin nh p l i s ", "L i nh p d txtdienthoai.Clear(); txtdienthoai.Focus(); } }

li u");

////////////////them///////////////////// private void btthem_Click(object sender, EventArgs e) { DataRow dong; dong = banghh.NewRow(); dong["tenncc"] = txttenhang.Text; dong["diachi"] = txtsoluongton.Text; dong["dienthoai"] = txtdongia.Text; banghh.Rows.Add(dong); //cap nhat CSDL capnhat = new SqlCommandBuilder(bodocghi); bodocghi.Update(banghh); banghh.Clear(); HienThiDL(); } /////////////////////////ham tinh tuoi///////////////////// public int tinhtuoi() { int tuoi = DateTime.Now.Year - namsinh; return tuoi; } ////////////sua//////////////////////////////// private void btsuact_Click(object sender, EventArgs e) { bangcthd.Rows[donghh]["id_hh"] = cbtenhang.SelectedValue; bangcthd.Rows[donghh]["soluongmua"] = txtsoluongmua.Text; try { capnhat = new SqlCommandBuilder(bodocghi); bodocghi.Update(bangcthd); bangcthd.Clear(); HienCTHoaDon(); } catch (Exception) { MessageBox.Show("S a chi ti t khng thnh cng","Thng bo"); } } ///////////xoa/////////////////////////////////////// private void btxoa_Click(object sender, EventArgs e) { bangcthd.Rows[donghh].Delete();

try { capnhat = new SqlCommandBuilder(bodocghi); bodocghi.Update(bangcthd); bangcthd.Clear(); HienCTHoaDon(); } catch(Exception) { MessageBox.Show("Xa chi ti t ha cng","Thng bo"); bangcthd.Clear(); HienCTHoaDon(); }

n khng thnh

You might also like