You are on page 1of 4

To mt lp thao tc vi csdl using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Data.SqlClient; System.

Data; System.Web.UI.WebControls;

public class clsDatabase { public clsDatabase() { // // TODO: Add constructor logic here // } // khai bo chui kt ni static string connectionString = @"Data Source=.;Initial Catalog=DangKy;Integrated Security=True"; SqlConnection con; SqlCommand cmd; //SqlDataReader dr; SqlDataAdapter da; //phng thc kt ni public void KetNoi() { con = new SqlConnection(connectionString); if (con.State == ConnectionState.Closed) con.Open(); } public void NgatKetNoi() { con.Close(); } // thc thi cu lnh QSL public void ThucThi(string Select) { KetNoi(); cmd = new SqlCommand(Select, con); cmd.ExecuteNonQuery(); NgatKetNoi(); } // m s bn ghi public int SoBanGhi(string select)

{ KetNoi(); cmd = new SqlCommand(select, con); int SBG = (int)(cmd.ExecuteScalar()); NgatKetNoi(); return SBG; } // Load d liu Ln gridview public void LoadGV(string select, GridView gv) { KetNoi(); DataTable dt = new DataTable(); da = new SqlDataAdapter(select, con); da.Fill(dt); gv.DataSource = dt; gv.DataBind(); NgatKetNoi(); } // Load du lieu len DropDownList public void LoadComBoBox(DropDownList dc, string select,string hien,string an) { DataTable dt = new DataTable(); da = new SqlDataAdapter(select, con); da.Fill(dt); dc.DataSource = dt; dc.DataTextField = hien; dc.DataValueField = an; dc.DataBind(); } } Vit code cho cc nt Thm, Sa Xa , Load du Liu ln Gridview, DropDownList using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class _Default : System.Web.UI.Page { clsDatabase db = new clsDatabase(); protected void Page_Load(object sender, EventArgs e)

{ if (!IsPostBack) { db.KetNoi(); // load ln GridView db.LoadGV("select * from TaiKhoan", gvsinhvien); // Load Ln DropDownList db.LoadComBoBox(cbload, "Select * from TaiKhoan ", "TenDN", "MatKhau"); db.NgatKetNoi(); } } // s kin Mi protected void Mi_Click(object sender, EventArgs e) { txtghichu.Text=""; txtmatkhau.Text=""; txttendangnhap.Text=""; } // s kin thm protected void Them_Click(object sender, EventArgs e) { string Them="insert into TaiKhoan values ('"+txttendangnhap.Text.Trim()+"','"+txtmatkhau.Text+"','"+txtghichu.T ext+"')"; db.ThucThi(Them); } // s kin sa protected void Sua_Click(object sender, EventArgs e) { string Sua = "Update TaiKhoan set MatKhau='" + txtmatkhau.Text.Trim() + "',GhiChu='" + txtghichu.Text.Trim() + "' where TenDN='" + txttendangnhap.Text.Trim() + "' "; db.ThucThi(Sua); } // s kin xa protected void xoa_Click(object sender, EventArgs e) { string Xoa = "Delete from TaiKhoan where TenDN='" + txttendangnhap.Text.Trim() + "'"; db.ThucThi(Xoa); } protected void cbload_SelectedIndexChanged(object sender, EventArgs e) {

} }

You might also like