SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 1
CHƯƠNG 5 – ADO.NET
T ng d ng, ta có th k t n i và thao tác v i cơ s d li u b ng 2 phương pháp sau:
1. K t n i thư ng xuyên
2. K t n i không thư ng xuyên
Ph n 1. K t n i thư ng xuyên
1. Các bư c th c hi n
Bư c 1: S d ng Connection k t n i n cơ s d li u
Bư c 2: Thi t l p câu l nh th c thi: Insert, Select, Update, Delete
Bư c 3: Th c hi n l nh
• M k t n i
• Th c thi câu l nh, x lý d li u tr v
• óng k t n i
2. Ví d m u
Thi t k giao di n g m các ph n như hình sau:
- Khi Load form các d li u t b ng Customers trong CSDL Northwind c a SQL Server
2000 s ư c hi n th trên ListView và DataGridView
- Khi ch n 1 dòng trên ListView ho c DataGridView, d li u c a dòng tương ng s hi n th
trên các TextBox
- Khi click vào nút Insert, d li u trong các Textbox ư c thêm vào cơ s d li u
- Khi click vào nút Update, record ư c ch n s ư c ch nh s a và c p nh t vào CSDL
- Khi click nút Delete, record ư c ch n s b xóa kh i CSDL
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 2
Ví d 1: c d li u t b ng Customers trong CSDL Northwind c a SQL Server 2000 và
hi n th lên ListView và DataGridView
// 1. Thi t l p k t n i
string strConn = "server=.; Database = Northwind; uid=sa; pwd=;";
SqlConnection cnNorth = new SqlConnection(strConn);
// 2. Thi t l p câu l nh
string sqlSelect = "select CustomerID, CompanyName, Address, City from
Customers";
SqlCommand cmdNorth = new SqlCommand(sqlSelect, cnNorth);
cmdNorth.Connection.Open();
// 3. Th c hi n l nh
SqlDataReader reader = cmdNorth.ExecuteReader();
// L y d li u hi n th , x lý... qua i tư ng Reader
// Xem ví d 1.1 ho c ví d 1.2
// …
// óng k t n i
cmdNorth.Connection.Close();
Ví d 1.1: o n chương trình sau mô t vi c c d li u t i tư ng reader và hi n th lên
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: o n chương trình sau mô t vi c c d li u t i tư ng reader và hi n th lên
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);
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 3
}
dataGridView1.DataSource = list;
Ví d 1.3: CustomerInfo là l p mô t các thông tin v i tư ng Customer. CustomerInfo
ư c vi t như sau:
public class CustomerInfo
{
string custId;
string contactName;
string custAddress;
string city;
public CustomerInfo()
{ }
public CustomerInfo(string custId, string contactName, string custAddress,
string city)
{
this.custId = custId;
this.contactName = contactName;
this.custAddress = custAddress;
this.city = city;
}
public string CustId
{
get {return custId;}
set {custId = value;}
}
public string ContactName
{
get {return contactName;}
set {contactName = value;}
}
public string CustAddress
{
get {return custAddress;}
set {custAddress = value;}
}
public string City
{
get {return city;}
set {city = value;}
}
}
Ví d 2: L y d li u t các Textbox: txtID, txtName, txt ddress và txtCity lưu vào Database
và c p nh t m i d li u hi n th trên form
private void cmdInsert_Click(object sender, System.EventArgs e)
{
// 1. K t n i
string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;";
SqlConnection cnNorth = new SqlConnection(strConn);
// 2. Thi t t câu l nh th c 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;
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 4
cmdNorth.Parameters[3].Value = txtCity.Text;
// 3. Th c thi l nh
cmdNorth.Connection.Open();
int kq = cmdNorth.ExecuteNonQuery();
if (kq > 0)
{
MessageBox.Show("D li u ã c p nh t!");
// G i l i hàm Load d li u Ví d 1
}
else
{
MessageBox.Show("Có l i xãy ra!");
}
cmdNorth.Connection.Close();
}
Ví d 3: Ch n 1 dòng trên ListView d li u tương ng s hi n th trên các TextBox.
private void lsvCustomer_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if (lsvCustomer.SelectedItems.Count == 0)
return;
CustomerInfo cm = lvCustomer.SelectedItems[0].Tag as CustomerInfo;
txtID.Text = cm.CustId;
txtName.Text = cm.ContactName;
txtAddress.Text = cm.CustAddress;
txtCity.Text = cm.City;
}
Ví d 4: Lưu d li u sau khi ã hi u ch nh trên TextBox vào CSDL
private void cmdUpdate_Click(object sender, System.EventArgs e)
{
if (lsvCustomer.SelectedItems.Count == 0)
return;
// L y thông tin v i tư ng ang ư c ch n
CustomerInfo old = lsvCustomer.SelectedItems[0].Tag as CustomerInfo;
// L y thông tin sau khi ã ch nh s a
CustomerInfo cm = new CustomerInfo(txtID.Text, txtName.Text,
txtAddress.Text, txtCity.Text);
// 1. i tư ng k t n i
string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;"
SqlConnection cnNorth = new SqlConnection(strConn);
// 2. Câu l nh th c thi
string sqlUpdate ="update Customers set CustomerID = "+
"@CustomerID, CompanyName = @CompanyName, Address = @Address, "+
"City = @City where CustomerID = @OrigCustomerID";
SqlCommand cmdNorth = new SqlCommand(sqlUpdate, 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.Add("@OrigCustomerID", SqlDbType.NChar);
cmdNorth.Parameters[0].Value = cm.CustId;
cmdNorth.Parameters[1].Value = cm.ContactName;
cmdNorth.Parameters[2].Value = cm.CustAddress;
cmdNorth.Parameters[3].Value = cm.City;
cmdNorth.Parameters[4].Value = old.CustId;
// 3. Th c thi l nh
cmdNorth.Connection.Open();
int kq = cmdNorth.ExecuteNonQuery();
if (kq > 0)
{
MessageBox.Show("C p nh t thành công!");
//G i l i phương th c Load d li u Ví d 1
}
else
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 5
MessageBox.Show("L i!");
cmdNorth.Connection.Close();
}
Ví d 5: Xóa dòng ư c ch n
private void cmdDelete_Click(object sender, System.EventArgs e)
{
if (lsvCustomer.SelectedItems.Count == 0)
return;
// L y thông tin v i tư ng ang ư c ch n
CustomerInfo cm = lsvCustomer.SelectedItems[0].Tag as CustomerInfo;
// 1. i tư ng k t n i
string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;"
SqlConnection cnNorth = new SqlConnection(strConn);
// 2. Câu l nh th c 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. Th c thi l nh
cmdNorth.Connection.Open();
int kq = cmdNorth.ExecuteNonQuery();
if (kq > 0)
{
MessageBox.Show("C p nh t thành công!");
//G i l i phương th c Load d li u Ví d 1
}
else
MessageBox.Show("L i!");
cmdNorth.Connection.Close();
}
3. Bài t p
Bài 1: Thi t k CSDL và Xây d ng ng d ng qu n lý thông tin khách hàng v i các yêu c u
sau:
- Form ăng nh p: ăng nh p trư c khi s d ng ng d ng
- Ki m tra d li u r ng trư c khi th c
hi n vi c x lý ăng nh p
- N u ăng nh p thành công thì cho phép
s d ng ph n Qu n lý
- Form Qu n lý: có giao di n như hình bên dư i, form này xem, thêm, s a, xóa thông tin
c a khách hàng. Các thông tin c n qu n lý bao g m: mã s , h tên, ngày sinh, a ch ,
i n tho i, email, hình nh
o Thông tin khách hàng s hi n th ngay khi vào form Qu n lý
o Thêm m i: thêm m i 1 khách hàng vào CSDL
o C p nh t: Ch nh s a thông tin 1 khách hàng trong CSDL
o Xóa: Xóa thông tin m t khách hàng
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 6
Ph n 2. K t n i không thư ng xuyên (Disconnected Architecture)
1. Các bư c th c hi n
Bư c 1: S d ng Connection k t n i n cơ s d li u
Bư c 2: T o i tư ng DataSet
Bư c 3: T o i tư ng DataAdapter và các câu l nh th c thi trên d li u
Bư c 4: d li u vào DataSet
Bư c 5: Tương tác, x lý d li u trên DataSet
Bư c 6: Lưu vào CSDL
2. Ví d m u
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 7
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);
//T o các câu l nh 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;
Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET
Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 8
txtTitle.Enabled = true;
txtAuthor.Enabled = true;
txtPublisher.Enabled = true;
txtPrice.Enabled = true;
}
private void cmdSave_Click(object sender, EventArgs e)
{
objDa.Update(ds,"Books");
}
}
3. M t s o n code m u
// 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. Bài t p
S d ng Disconnected Architecture làm l i bài t p Ph n 1

Más contenido relacionado

Destacado

Destacado (16)

Caribatars
Caribatars Caribatars
Caribatars
 
Resume satyendra
Resume satyendraResume satyendra
Resume satyendra
 
Basics of r commands
Basics of r commandsBasics of r commands
Basics of r commands
 
Música do pas - Monólogo ao pé do ouvido
Música do pas - Monólogo ao pé do ouvidoMúsica do pas - Monólogo ao pé do ouvido
Música do pas - Monólogo ao pé do ouvido
 
642402 634249935653553750
642402 634249935653553750642402 634249935653553750
642402 634249935653553750
 
Macc nut swot
Macc nut swotMacc nut swot
Macc nut swot
 
Animals
AnimalsAnimals
Animals
 
L auge de_l_artesania
L auge de_l_artesaniaL auge de_l_artesania
L auge de_l_artesania
 
Algebra Foundations Series- 1.2 Order of Operations
Algebra Foundations Series- 1.2 Order of OperationsAlgebra Foundations Series- 1.2 Order of Operations
Algebra Foundations Series- 1.2 Order of Operations
 
gigko v v
gigko v vgigko v v
gigko v v
 
Osidna
OsidnaOsidna
Osidna
 
Cruise city break 2013 thursday
Cruise city break 2013 thursdayCruise city break 2013 thursday
Cruise city break 2013 thursday
 
Aleluia
AleluiaAleluia
Aleluia
 
Used car parts : The Smart Choice
Used car parts : The Smart ChoiceUsed car parts : The Smart Choice
Used car parts : The Smart Choice
 
Powering the People
Powering the PeoplePowering the People
Powering the People
 
Windows 7,8,10
Windows 7,8,10Windows 7,8,10
Windows 7,8,10
 

Similar a Hdth.chuong5 ado.netv2.0

Cq lt hdt-th2011-02-tuan04
Cq lt hdt-th2011-02-tuan04Cq lt hdt-th2011-02-tuan04
Cq lt hdt-th2011-02-tuan04. .
 
De cuongthuchanhct221 hk2_1920_n1
De cuongthuchanhct221 hk2_1920_n1De cuongthuchanhct221 hk2_1920_n1
De cuongthuchanhct221 hk2_1920_n1Nahudi Nguyễn
 
Web Services
Web ServicesWeb Services
Web Servicesask bills
 
7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)TI Anh
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databindingtruong le hung
 
Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Dũng Đinh
 
Mô hình hóa yêu cầu
Mô hình hóa yêu cầuMô hình hóa yêu cầu
Mô hình hóa yêu cầuNguyen Tran
 
Meo lap trinh_tech24.vn
Meo lap trinh_tech24.vnMeo lap trinh_tech24.vn
Meo lap trinh_tech24.vnphiagame
 
Trần Anh Khoa - Kautilya và Powershell trong kỹ thuật tấn công tiếp cận
Trần Anh Khoa - Kautilya và Powershelltrong kỹ thuật tấn công tiếp cậnTrần Anh Khoa - Kautilya và Powershelltrong kỹ thuật tấn công tiếp cận
Trần Anh Khoa - Kautilya và Powershell trong kỹ thuật tấn công tiếp cậnSecurity Bootcamp
 
Cau hoi trac nghiem lt nc (1)
Cau hoi trac nghiem lt nc (1)Cau hoi trac nghiem lt nc (1)
Cau hoi trac nghiem lt nc (1)Pharmacist Ctump
 
Net06 asp.net applications & state management
Net06 asp.net applications & state managementNet06 asp.net applications & state management
Net06 asp.net applications & state managementhoangnguyentien
 

Similar a Hdth.chuong5 ado.netv2.0 (20)

Asp
AspAsp
Asp
 
access
accessaccess
access
 
Cq lt hdt-th2011-02-tuan04
Cq lt hdt-th2011-02-tuan04Cq lt hdt-th2011-02-tuan04
Cq lt hdt-th2011-02-tuan04
 
De cuongthuchanhct221 hk2_1920_n1
De cuongthuchanhct221 hk2_1920_n1De cuongthuchanhct221 hk2_1920_n1
De cuongthuchanhct221 hk2_1920_n1
 
Cach su dung data reader
Cach su dung data readerCach su dung data reader
Cach su dung data reader
 
Web Services
Web ServicesWeb Services
Web Services
 
7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)7 databinding-120306231825-phpapp02(1)
7 databinding-120306231825-phpapp02(1)
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databinding
 
Bai th 03
Bai th 03Bai th 03
Bai th 03
 
Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1Hdth07 ltudql02-linq-ep1
Hdth07 ltudql02-linq-ep1
 
Mô hình hóa yêu cầu
Mô hình hóa yêu cầuMô hình hóa yêu cầu
Mô hình hóa yêu cầu
 
Meo lap trinh_tech24.vn
Meo lap trinh_tech24.vnMeo lap trinh_tech24.vn
Meo lap trinh_tech24.vn
 
Bai tap thuc hanh
Bai tap thuc hanhBai tap thuc hanh
Bai tap thuc hanh
 
Trần Anh Khoa - Kautilya và Powershell trong kỹ thuật tấn công tiếp cận
Trần Anh Khoa - Kautilya và Powershelltrong kỹ thuật tấn công tiếp cậnTrần Anh Khoa - Kautilya và Powershelltrong kỹ thuật tấn công tiếp cận
Trần Anh Khoa - Kautilya và Powershell trong kỹ thuật tấn công tiếp cận
 
Cau hoi trac nghiem lt nc (1)
Cau hoi trac nghiem lt nc (1)Cau hoi trac nghiem lt nc (1)
Cau hoi trac nghiem lt nc (1)
 
User Control
User ControlUser Control
User Control
 
Linq2 sql
Linq2 sqlLinq2 sql
Linq2 sql
 
Net06 asp.net applications & state management
Net06 asp.net applications & state managementNet06 asp.net applications & state management
Net06 asp.net applications & state management
 
Lap trinhvba
Lap trinhvbaLap trinhvba
Lap trinhvba
 
Ktlt lab full
Ktlt lab fullKtlt lab full
Ktlt lab full
 

Último

SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...
SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...
SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...Nguyen Thanh Tu Collection
 
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...Nguyen Thanh Tu Collection
 
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...Nguyen Thanh Tu Collection
 
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...Nguyen Thanh Tu Collection
 
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...Nguyen Thanh Tu Collection
 
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...Nguyen Thanh Tu Collection
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...Nguyen Thanh Tu Collection
 
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...Nguyen Thanh Tu Collection
 
Day tieng Viet cho nguoi nuoc ngoai.pptx
Day tieng Viet cho nguoi nuoc ngoai.pptxDay tieng Viet cho nguoi nuoc ngoai.pptx
Day tieng Viet cho nguoi nuoc ngoai.pptxngothevinhs6lite
 
lịch sử đảng cộng sản việt nam chương 1.ppt
lịch sử đảng cộng sản việt nam chương 1.pptlịch sử đảng cộng sản việt nam chương 1.ppt
lịch sử đảng cộng sản việt nam chương 1.pptLinhPham480
 
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...Nguyen Thanh Tu Collection
 
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...Nguyen Thanh Tu Collection
 
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...Nguyen Thanh Tu Collection
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...Nguyen Thanh Tu Collection
 
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...Nguyen Thanh Tu Collection
 
IELTS READING - Earth’s lakes are under threat.pptx
IELTS READING - Earth’s lakes are under threat.pptxIELTS READING - Earth’s lakes are under threat.pptx
IELTS READING - Earth’s lakes are under threat.pptxNguynHn870045
 

Último (17)

SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...
SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...
SÁNG KIẾN PHÁT TRIỂN NĂNG LỰC TỰ LÀM MÔ HÌNH KHI TÌM HIỂU KIẾN THỨC “THẠCH QU...
 
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY SINH HỌC 10 CHÂN TRỜI SÁNG TẠO - CẢ NĂM THEO CÔNG VĂ...
 
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...
BÀI TẬP – BÀI GIẢI HÓA HỮU CƠ – TẬP 1 DÙNG BỒI DƯỠNG HỌC SINH GIỎI TỈNH VÀ QU...
 
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...
HƯỚNG DẪN GIẢI ĐỀ THI THAM KHẢO KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤ...
 
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...
40 ĐỀ LUYỆN THI ĐÁNH GIÁ NĂNG LỰC ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH NĂM ...
 
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...
CHUYÊN ĐỀ DẠY THÊM HÓA HỌC LỚP 11 CHUNG 3 BỘ SÁCH NĂM 2024 HỆ THỐNG BÀI TẬP B...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 CẢ NĂM CÓ TEST ÔN TẬP ĐỊNH KÌ + NÂNG CAO - FRI...
 
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...
GIÁO ÁN KẾ HOẠCH BÀI DẠY MÔN VẬT LÝ 11 CẢ NĂM (SÁCH KẾT NỐI TRI THỨC) THEO CÔ...
 
Day tieng Viet cho nguoi nuoc ngoai.pptx
Day tieng Viet cho nguoi nuoc ngoai.pptxDay tieng Viet cho nguoi nuoc ngoai.pptx
Day tieng Viet cho nguoi nuoc ngoai.pptx
 
lịch sử đảng cộng sản việt nam chương 1.ppt
lịch sử đảng cộng sản việt nam chương 1.pptlịch sử đảng cộng sản việt nam chương 1.ppt
lịch sử đảng cộng sản việt nam chương 1.ppt
 
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...
14 CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN VẬT LÝ 8 - NĂM 2024 (4...
 
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...
40 ĐỀ THI THỬ TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2024 - 2025 SỞ GIÁO...
 
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...
ĐỀ KIỂM TRA THEO UNIT TIẾNG ANH GLOBAL SUCCESS 11 - HK2 (BẢN HS-GV) (3 TESTS ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...
CHUYÊN ĐỀ BỒI DƯỠNG HỌC SINH GIỎI KHOA HỌC TỰ NHIÊN 7 + 8 CHƯƠNG TRÌNH GDPT M...
 
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...
HƯỚNG DẪN GIẢI ĐỀ MINH HỌA KÌ THI TỐT NGHIỆP THPT NĂM 2024 TỪ BỘ GIÁO DỤC MÔN...
 
IELTS READING - Earth’s lakes are under threat.pptx
IELTS READING - Earth’s lakes are under threat.pptxIELTS READING - Earth’s lakes are under threat.pptx
IELTS READING - Earth’s lakes are under threat.pptx
 

Hdth.chuong5 ado.netv2.0

  • 1. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 1 CHƯƠNG 5 – ADO.NET T ng d ng, ta có th k t n i và thao tác v i cơ s d li u b ng 2 phương pháp sau: 1. K t n i thư ng xuyên 2. K t n i không thư ng xuyên Ph n 1. K t n i thư ng xuyên 1. Các bư c th c hi n Bư c 1: S d ng Connection k t n i n cơ s d li u Bư c 2: Thi t l p câu l nh th c thi: Insert, Select, Update, Delete Bư c 3: Th c hi n l nh • M k t n i • Th c thi câu l nh, x lý d li u tr v • óng k t n i 2. Ví d m u Thi t k giao di n g m các ph n như hình sau: - Khi Load form các d li u t b ng Customers trong CSDL Northwind c a SQL Server 2000 s ư c hi n th trên ListView và DataGridView - Khi ch n 1 dòng trên ListView ho c DataGridView, d li u c a dòng tương ng s hi n th trên các TextBox - Khi click vào nút Insert, d li u trong các Textbox ư c thêm vào cơ s d li u - Khi click vào nút Update, record ư c ch n s ư c ch nh s a và c p nh t vào CSDL - Khi click nút Delete, record ư c ch n s b xóa kh i CSDL
  • 2. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 2 Ví d 1: c d li u t b ng Customers trong CSDL Northwind c a SQL Server 2000 và hi n th lên ListView và DataGridView // 1. Thi t l p k t n i string strConn = "server=.; Database = Northwind; uid=sa; pwd=;"; SqlConnection cnNorth = new SqlConnection(strConn); // 2. Thi t l p câu l nh string sqlSelect = "select CustomerID, CompanyName, Address, City from Customers"; SqlCommand cmdNorth = new SqlCommand(sqlSelect, cnNorth); cmdNorth.Connection.Open(); // 3. Th c hi n l nh SqlDataReader reader = cmdNorth.ExecuteReader(); // L y d li u hi n th , x lý... qua i tư ng Reader // Xem ví d 1.1 ho c ví d 1.2 // … // óng k t n i cmdNorth.Connection.Close(); Ví d 1.1: o n chương trình sau mô t vi c c d li u t i tư ng reader và hi n th lên 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: o n chương trình sau mô t vi c c d li u t i tư ng reader và hi n th lên 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);
  • 3. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 3 } dataGridView1.DataSource = list; Ví d 1.3: CustomerInfo là l p mô t các thông tin v i tư ng Customer. CustomerInfo ư c vi t như sau: public class CustomerInfo { string custId; string contactName; string custAddress; string city; public CustomerInfo() { } public CustomerInfo(string custId, string contactName, string custAddress, string city) { this.custId = custId; this.contactName = contactName; this.custAddress = custAddress; this.city = city; } public string CustId { get {return custId;} set {custId = value;} } public string ContactName { get {return contactName;} set {contactName = value;} } public string CustAddress { get {return custAddress;} set {custAddress = value;} } public string City { get {return city;} set {city = value;} } } Ví d 2: L y d li u t các Textbox: txtID, txtName, txt ddress và txtCity lưu vào Database và c p nh t m i d li u hi n th trên form private void cmdInsert_Click(object sender, System.EventArgs e) { // 1. K t n i string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;"; SqlConnection cnNorth = new SqlConnection(strConn); // 2. Thi t t câu l nh th c 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;
  • 4. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 4 cmdNorth.Parameters[3].Value = txtCity.Text; // 3. Th c thi l nh cmdNorth.Connection.Open(); int kq = cmdNorth.ExecuteNonQuery(); if (kq > 0) { MessageBox.Show("D li u ã c p nh t!"); // G i l i hàm Load d li u Ví d 1 } else { MessageBox.Show("Có l i xãy ra!"); } cmdNorth.Connection.Close(); } Ví d 3: Ch n 1 dòng trên ListView d li u tương ng s hi n th trên các TextBox. private void lsvCustomer_SelectedIndexChanged(object sender, System.EventArgs e) { if (lsvCustomer.SelectedItems.Count == 0) return; CustomerInfo cm = lvCustomer.SelectedItems[0].Tag as CustomerInfo; txtID.Text = cm.CustId; txtName.Text = cm.ContactName; txtAddress.Text = cm.CustAddress; txtCity.Text = cm.City; } Ví d 4: Lưu d li u sau khi ã hi u ch nh trên TextBox vào CSDL private void cmdUpdate_Click(object sender, System.EventArgs e) { if (lsvCustomer.SelectedItems.Count == 0) return; // L y thông tin v i tư ng ang ư c ch n CustomerInfo old = lsvCustomer.SelectedItems[0].Tag as CustomerInfo; // L y thông tin sau khi ã ch nh s a CustomerInfo cm = new CustomerInfo(txtID.Text, txtName.Text, txtAddress.Text, txtCity.Text); // 1. i tư ng k t n i string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;" SqlConnection cnNorth = new SqlConnection(strConn); // 2. Câu l nh th c thi string sqlUpdate ="update Customers set CustomerID = "+ "@CustomerID, CompanyName = @CompanyName, Address = @Address, "+ "City = @City where CustomerID = @OrigCustomerID"; SqlCommand cmdNorth = new SqlCommand(sqlUpdate, 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.Add("@OrigCustomerID", SqlDbType.NChar); cmdNorth.Parameters[0].Value = cm.CustId; cmdNorth.Parameters[1].Value = cm.ContactName; cmdNorth.Parameters[2].Value = cm.CustAddress; cmdNorth.Parameters[3].Value = cm.City; cmdNorth.Parameters[4].Value = old.CustId; // 3. Th c thi l nh cmdNorth.Connection.Open(); int kq = cmdNorth.ExecuteNonQuery(); if (kq > 0) { MessageBox.Show("C p nh t thành công!"); //G i l i phương th c Load d li u Ví d 1 } else
  • 5. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 5 MessageBox.Show("L i!"); cmdNorth.Connection.Close(); } Ví d 5: Xóa dòng ư c ch n private void cmdDelete_Click(object sender, System.EventArgs e) { if (lsvCustomer.SelectedItems.Count == 0) return; // L y thông tin v i tư ng ang ư c ch n CustomerInfo cm = lsvCustomer.SelectedItems[0].Tag as CustomerInfo; // 1. i tư ng k t n i string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;" SqlConnection cnNorth = new SqlConnection(strConn); // 2. Câu l nh th c 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. Th c thi l nh cmdNorth.Connection.Open(); int kq = cmdNorth.ExecuteNonQuery(); if (kq > 0) { MessageBox.Show("C p nh t thành công!"); //G i l i phương th c Load d li u Ví d 1 } else MessageBox.Show("L i!"); cmdNorth.Connection.Close(); } 3. Bài t p Bài 1: Thi t k CSDL và Xây d ng ng d ng qu n lý thông tin khách hàng v i các yêu c u sau: - Form ăng nh p: ăng nh p trư c khi s d ng ng d ng - Ki m tra d li u r ng trư c khi th c hi n vi c x lý ăng nh p - N u ăng nh p thành công thì cho phép s d ng ph n Qu n lý - Form Qu n lý: có giao di n như hình bên dư i, form này xem, thêm, s a, xóa thông tin c a khách hàng. Các thông tin c n qu n lý bao g m: mã s , h tên, ngày sinh, a ch , i n tho i, email, hình nh o Thông tin khách hàng s hi n th ngay khi vào form Qu n lý o Thêm m i: thêm m i 1 khách hàng vào CSDL o C p nh t: Ch nh s a thông tin 1 khách hàng trong CSDL o Xóa: Xóa thông tin m t khách hàng
  • 6. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 6 Ph n 2. K t n i không thư ng xuyên (Disconnected Architecture) 1. Các bư c th c hi n Bư c 1: S d ng Connection k t n i n cơ s d li u Bư c 2: T o i tư ng DataSet Bư c 3: T o i tư ng DataAdapter và các câu l nh th c thi trên d li u Bư c 4: d li u vào DataSet Bư c 5: Tương tác, x lý d li u trên DataSet Bư c 6: Lưu vào CSDL 2. Ví d m u
  • 7. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 7 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); //T o các câu l nh 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;
  • 8. Hư ng d n th c hành Winforms v i C# Chương 5: ADO.NET Hue-Aptech | Tr n Văn Long – Email: tvlongsp@gmail.com Trang 8 txtTitle.Enabled = true; txtAuthor.Enabled = true; txtPublisher.Enabled = true; txtPrice.Enabled = true; } private void cmdSave_Click(object sender, EventArgs e) { objDa.Update(ds,"Books"); } } 3. M t s o n code m u // 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. Bài t p S d ng Disconnected Architecture làm l i bài t p Ph n 1