Lập trình thực hành với C++ - Pdf 27

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: [email protected] 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

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);

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;}

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;

}
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: [email protected] 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)
{


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status