0% found this document useful (0 votes)
55 views

Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for a C# application that manages student data stored in a SQL database. It includes functions for loading student data from the database into a datagrid, inserting new students, updating and deleting existing student records, and handling user interface events like clicking buttons or selecting rows in the datagrid. The code uses ADO.NET and stored procedures to perform CRUD operations on the student data in the database.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for a C# application that manages student data stored in a SQL database. It includes functions for loading student data from the database into a datagrid, inserting new students, updating and deleting existing student records, and handling user interface events like clicking buttons or selecting rows in the datagrid. The code uses ADO.NET and stored procedures to perform CRUD operations on the student data in the database.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.SqlClient;

namespace ADOThucTap { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } // truyn d liu private void loaddata() { string strCon = @"server=(local); database=ThuTap1; integrated security=true;"; SqlConnection sqlCon = new SqlConnection(strCon); try { sqlCon.Open(); string strSQL = "SinhVien_SelectAll"; SqlCommand cmd = new SqlCommand(strSQL, sqlCon); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dtsinhvien = new DataTable(); da.Fill(dtsinhvien); dgvSinhVien.DataSource = dtsinhvien; inSTT(); } catch (Exception Ex) { MessageBox.Show("loi: " + Ex.ToString()); } finally { sqlCon.Close(); } } //thm private void themsinhvien(string strMaSV, string strTenSV, string strDiachi) { string strCon = @"server=(local); database=ThuTap1; integrated security=true;"; SqlConnection sqlCon = new SqlConnection(strCon); try { sqlCon.Open(); string strSQL = "SinhVien_Insert";

SqlCommand cmd = new SqlCommand(strSQL, sqlCon); cmd.CommandType = CommandType.StoredProcedure; // cmd.Parameters.Add("@MaM", SqlDbType.VarChar, 50).Value = MaM1; cmd.Parameters.Add("@MaSV", SqlDbType.NVarChar, 50).Value = strMaSV; cmd.Parameters.Add("@HoTenSV", SqlDbType.NVarChar, 50).Value = strTenSV; cmd.Parameters.Add("@DiaChi",SqlDbType.NVarChar,50).Value=strDiachi; cmd.ExecuteNonQuery(); // loaddata(); } catch (Exception Ex) { MessageBox.Show("Loi: " + Ex.ToString()); } finally { sqlCon.Close(); } } //gi d liu ra form chnh private void Form1_Load(object sender, EventArgs e) { loaddata(); } //hin th s th t public void inSTT() { for (int i = 0; i < dgvSinhVien.Rows.Count; i++) dgvSinhVien["STT", i].Value = (i + 1); } // buttum thm private void btnthem_Click(object sender, EventArgs e) { string strMaSV = txtMaSV.Text.Trim(); string strTenSV = txtTenSV.Text.Trim(); string strDiachi = txtDiaChi.Text.Trim(); themsinhvien(strMaSV, strTenSV, strDiachi); loaddata(); } private void value_select(int index) { DataGridViewRow row = dgvSinhVien.Rows[index]; txtMaSV.Text = row.Cells["MaSV"].Value.ToString(); txtTenSV.Text = row.Cells["HoTenSV"].Value.ToString(); //dtmNgaysinh.Value.ToString() = row.Cells["NS"].Value.ToString(); txtDiaChi.Text = row.Cells["DiaChi"].Value.ToString(); } private void dgvSinhVien_RowEnter(object sender, DataGridViewCellEventArgs e) { value_select(e.RowIndex); } // th tc xa private void xoa(string strMasv)

{ if (MessageBox.Show("Bn c mun xa khng???", "thng bo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; string strCon = @"server=(local); database=ThuTap1; integrated security=true;"; SqlConnection sqlCon = new SqlConnection(strCon); try { sqlCon.Open(); string strSQL = "SinhVien_Delete"; SqlCommand cmd = new SqlCommand(strSQL, sqlCon); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@MaSV", strMasv); cmd.ExecuteNonQuery(); loaddata(); } catch (Exception Ex) { MessageBox.Show("Loi: " + Ex.ToString()); } finally { sqlCon.Close(); }

} // button xoa private void btndelete_Click(object sender, EventArgs e) { string strMaSV=txtMaSV.Text.Trim(); xoa(strMaSV); } // integrated security=true;"; // th tc sa private void update(string strMaSV,string strHoTen,string strDiaChi) { string strCon = @"server=(local); database=ThuTap1; integrated security=true;"; SqlConnection sqlCon = new SqlConnection(strCon); try { sqlCon.Open(); string strSQL="SinhVien_Update"; SqlCommand cmd = new SqlCommand(strSQL, sqlCon); cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@MaSV", strMaSV); cmd.Parameters.Add("@HoTenSV", strHoTen); cmd.Parameters.Add("@DiaChi", strDiaChi); cmd.ExecuteNonQuery(); loaddata();

} catch (Exception Ex) { MessageBox.Show("Loi: " + Ex.ToString()); } finally { sqlCon.Close(); }

} // button sa private void btnupdate_Click(object sender, EventArgs e) { string strMaSV = txtMaSV.Text.Trim(); string strTen = txtTenSV.Text.Trim(); string strdiachi = txtDiaChi.Text.Trim(); update(strMaSV,strTen,strdiachi); } } }

You might also like