1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Hư ng d n th c hành Winforms v i C#Chương 5: ADO.NETCHƯƠNG 5 – ADO.NETT ng d ng, ta có potx

8 326 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 410,71 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Thiết lập câu lệnh string sqlSelect = "select CustomerID, CompanyName, Address, City from Customers"; SqlCommand cmdNorth = new SqlCommandsqlSelect, cnNorth; // 3.. Thực hiện lệnh cmdNo

Trang 1

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 1

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

Phn 1 K ế t ni th ườ ng xuyên (Conected Architechture)

1 Các b ướ c thc hin

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

o M ở k ế t n ố i

o Th ự c hi ệ n l ệ nh

o X ử lý d ữ li ệ u tr ả v ề

o Đ óng k ế t n ố i

2 Ví d mu

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

Trang 2

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 2

- 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

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

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

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

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

}

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

// 3 Thực hiện lệnh

cmdNorth.Connection.Open();

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

Trang 3

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 3

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:

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

}

dataGridView1.DataSource = list;

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

} }

Trang 4

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 4

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

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;

}

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;

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

// Gi li hàm Load d liu Ví d 1

}

else

{

MessageBox.Show("Có lỗi xãy ra!");

}

cmdNorth.Connection.Close();

}

Trang 5

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 5

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

//Gi li phương thc Load d liu Ví d 1

}

else

MessageBox.Show("Lỗi!");

cmdNorth.Connection.Close();

}

Trang 6

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 6

Ví d 5: Xóa dòng đượ c ch ọ n

3 Bài tp

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

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

//Gi li phương thc Load d liu Ví d 1

}

else

MessageBox.Show("Lỗi!");

cmdNorth.Connection.Close();

}

Trang 7

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 7

Phn 2 K ế t ni không th ườ ng xuyên (Disconected Architechture)

1 Các b ướ c thc hin

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: Tao đố i t ượ ng DataAdapter và các câu l ệ nh th ự c thi d ữ li ệ u

B ướ c 4: Đỗ d ữ li ệ u vào DataSet

B ướ c 5: T ươ ng tác d ữ li ệ u trên DataSet

B ướ c 6: C ậ p nh ậ t d ữ li ệ u t ừ DataSet lên Server

2 Mt s ố đ on code mu

Trang 8

Hue-Aptech | Tr ầ n V ă n Long – Email: tvlongsp@gmail.com Trang 8

// 1 Kết nối

string strConn = "server=(local); Database = Northwind; uid=sa; pwd=;"; // 2 Tạo đôi tượng DataSet

DataSet dsCustomers = new DataSet();

// 3 Tạo đôi tượng DataAdapter và các câu lệnh thực thi dữ liệu

SqlDataAdapter daCustomers = new SqlDataAdapter(

"select CustomerID, CompanyName from Customers", conn);

//Sử dụng SqlCommandBuider để xây dựng các câu lệnh Insert, Update, Delete

SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);

// 4 Đỗ dữ liệu vào DataSet

daCustomers.Fill(dsCustomers, "Customers");

// 5 Tương tác với dữ liệu trên DataSet

dgCustomers.DataSource = dsCustomers;

dgCustomers.DataMember = "Customers";

// 6 Cập nhật dữ liệu từ DataSet lên Server

daCustomers.Update(dsCustomers, "Customers");

Ngày đăng: 29/06/2014, 03:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm