Professional Documents
Culture Documents
Chng 5: ADO.NET
CHNG 5 ADO.NET
T ng dng, ta c th kt ni v thao tc vi c s d liu bng 2 phng php sau: 1. Kt ni thng xuyn 2. Kt ni khng thng xuyn
2. V d mu
Thit k giao din gm cc phn nh hnh sau:
Khi Load form cc d liu t bng Customers trong CSDL Northwind ca SQL Server 2000 s c hin th trn ListView v DataGridView Khi chn 1 dng trn ListView hoc DataGridView, d liu ca dng tng ng s hin th trn cc TextBox Khi click vo nt Insert, d liu trong cc Textbox c thm vo c s d liu Khi click vo nt Update, record c chn s c chnh sa v cp nht vo CSDL Khi click nt Delete, record c chn s b xa khi CSDL
Chng 5: ADO.NET
V d 1: c d liu t bng Customers trong CSDL Northwind ca SQL Server 2000 v hin th ln ListView v DataGridView
// 1. Thit lp kt ni string strConn = "server=.; Database = Northwind; uid=sa; pwd=;"; SqlConnection cnNorth = new SqlConnection(strConn); // 2. Thit lp cu lnh string sqlSelect = "select CustomerID, CompanyName, Address, City from Customers"; SqlCommand cmdNorth = new SqlCommand(sqlSelect, cnNorth); cmdNorth.Connection.Open(); // 3. Thc hin lnh SqlDataReader reader = cmdNorth.ExecuteReader();
// Ly d liu hin th, x l... qua i tng Reader // Xem v d 1.1 hoc v d 1.2 // // ng kt ni cmdNorth.Connection.Close();
V d 1.1: on chng trnh sau m t vic c d liu t i tng reader v hin th ln ListView
CustomerInfo cm; // Xem v d 1.3 while (reader.Read()) { cm = new CustomerInfo(); cm.CustId = reader.GetString(0); cm.ContactName = reader.GetString(1); if (reader.IsDBNull(2)) cm.CustAddress = ""; else cm.CustAddress =reader.GetString(2);
if (reader.IsDBNull(3)) cm.City = ""; else cm.City =reader.GetString(3); ListViewItem lvItem = new ListViewItem(cm.CustId); lvItem.SubItems.Add(cm.ContactName); lvItem.SubItems.Add(cm.CustAddress); lvItem.SubItems.Add(cm.City); lvItem.Tag = cm; lsvCustomer.Items.Add(lvItem);
}
V d 1.2: on chng trnh sau m t vic c d liu t i tng reader v hin th ln DataGridView
ArrayList list = new ArrayList();
CustomerInfo cm; // Xem v d 1.3 while (reader.Read()) { cm = new CustomerInfo(); cm.CustId = reader.GetString(0); cm.ContactName = reader.GetString(1); if (reader.IsDBNull(2)) cm.CustAddress = ""; else cm.CustAddress =reader.GetString(2);
if (reader.IsDBNull(3)) cm.City = ""; else cm.City =reader.GetString(3);
list.Add(cm);
Chng 5: ADO.NET
dataGridView1.DataSource = list;
V d 2: Ly d liu t cc Textbox: txtID, txtName, txtddress v txtCity lu vo Database v cp nht mi d liu hin th trn form
private void cmdInsert_Click(object sender, System.EventArgs e) { // 1. Kt ni string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;"; SqlConnection cnNorth = new SqlConnection(strConn); // 2. Thit t cu lnh thc thi string sqlInsert= "insert into Customers(CustomerID, " + "CompanyName, Address, City) values(@CustomerID, @CompanyName, "+ "@Address, @City)"; SqlCommand cmdNorth = new SqlCommand(sqlInsert, cnNorth); cmdNorth.Parameters.Add("@CustomerID", SqlDbType.NChar); cmdNorth.Parameters.Add("@CompanyName", SqlDbType.NChar); cmdNorth.Parameters.Add("@Address", SqlDbType.NChar); cmdNorth.Parameters.Add("@City", SqlDbType.NChar); cmdNorth.Parameters[0].Value = txtID.Text; cmdNorth.Parameters[1].Value = txtName.Text; cmdNorth.Parameters[2].Value = txtAddress.Text;
Chng 5: ADO.NET
Chng 5: ADO.NET
V d 5: Xa dng c chn
private void cmdDelete_Click(object sender, System.EventArgs e) { if (lsvCustomer.SelectedItems.Count == 0) return; // Ly thng tin v i tng ang c chn CustomerInfo cm = lsvCustomer.SelectedItems[0].Tag as CustomerInfo; // 1. i tng kt ni string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;" SqlConnection cnNorth = new SqlConnection(strConn); // 2. Cu lnh thc thi string sqlUpdate ="Delete from Customers where CustomerID=@CustomerID"; SqlCommand cmdNorth = new SqlCommand(sqlUpdate, cnNorth); cmdNorth.Parameters.Add("@CustomerID", SqlDbType.NChar); cmdNorth.Parameters[0].Value = cm.CustId; // 3. Thc thi lnh cmdNorth.Connection.Open(); int kq = cmdNorth.ExecuteNonQuery(); if (kq > 0) { MessageBox.Show("Cp nht thnh cng!"); //Gi li phng thc Load d liu V d 1 } else MessageBox.Show("Li!"); cmdNorth.Connection.Close(); }
3. Bi tp
Bi 1: Thit k CSDL v Xy dng ng dng qun l thng tin khch hng vi cc yu cu sau: - Form ng nhp: ng nhp trc khi s dng ng dng
Kim tra d liu rng trc khi thc hin vic x l ng nhp Nu ng nhp thnh cng th cho php s dng phn Qun l
Form Qun l: c giao din nh hnh bn di, form ny xem, thm, sa, xa thng tin ca khch hng. Cc thng tin cn qun l bao gm: m s, h tn, ngy sinh, a ch, in thoi, email, hnh nh o Thng tin khch hng s hin th ngay khi vo form Qun l o Thm mi: thm mi 1 khch hng vo CSDL o Cp nht: Chnh sa thng tin 1 khch hng trong CSDL o Xa: Xa thng tin mt khch hng
Chng 5: ADO.NET
Chng 5: ADO.NET
public partial class Form1 : Form { private DataSet ds; private SqlConnection objConn; private SqlDataAdapter objDa; private string STRCONN = "Server=.;Database=BMS;uid=sa;pwd=;"; public Form1() { InitializeComponent();
} private void loadData() { objConn = new SqlConnection(STRCONN); ds = new DataSet();
objDa = new SqlDataAdapter("SELECT * FROM Books", objConn);
//To cc cu lnh Insert, Update, Delete t ng SqlCommandBuilder cmb = new SqlCommandBuilder(objDa);
objDa.Fill(ds, "Books");
//Do du lieu len DataGridView dataGridView1.DataSource = ds; dataGridView1.DataMember = "Books";
//Bind du lieu len TextBox txtID.DataBindings.Add("Text", ds, "Books.BookID"); txtTypeID.DataBindings.Add("Text", ds, "Books.TypeID"); txtTitle.DataBindings.Add("Text", ds, "Books.Title"); txtPublisher.DataBindings.Add("Text", ds, "Books.Publisher"); txtAuthor.DataBindings.Add("Text", ds, "Books.Author"); txtPrice.DataBindings.Add("Text", ds, "Books.Price");
} private void Form1_Load(object sender, EventArgs e) { loadData(); }
private void cmdDelete_Click(object sender, EventArgs e) { int i = (int)this.BindingContext[ds, "Books"].Position; ds.Tables[0].Rows[i].Delete(); objDa.Update(ds, "Books"); } private void cmdAddNew_Click(object sender, EventArgs e) { txtID.Enabled = true; txtTypeID.Enabled = true; txtTitle.Enabled = true; txtAuthor.Enabled = true; txtPublisher.Enabled = true; txtPrice.Enabled = true; this.BindingContext[ds, "Books"].AddNew(); }
private void cmdUpdate_Click(object sender, EventArgs e) { //txtID.Enabled = true; txtTypeID.Enabled = true;
Chng 5: ADO.NET
3. Mt s on code mu
// Get current Rowposition CurrencyManager cm = (CurrencyManager)this.BindingContext[ds,"Books"]; long rowPosition = (long)cm.Position;
// Combobox Databinding cboTypeID.DataSource = ds; cboTypeID.DisplayMember = "Books.TypeName"; cboTypeID.ValueMember = "Books.TypeID";
// Position to prev Record in Customer private void btnPrev_Click(object sender, System.EventArgs e) { if (this.BindingContext[ds,"Books"].Position > 0) { this.BindingContext[ds,"Books"].Position--; } } // Position to next Record in Customer private void btnNext_Click(object sender, System.EventArgs e) { CurrencyManager cm = (CurrencyManager)this.BindingContext[ds,"Books"]; if (cm.Position < cm.Count - 1) { cm.Position++; } }
4. Bi tp
S dng Disconnected Architecture lm li bi tp Phn 1