tailieunhanh - Beginning C# 2005 Databases From Novice to Professional phần 5

Nhấn Finish, và bạn sẽ quay trở lại cửa sổ trong hình 9-3. Bạn không cần phải làm bất cứ điều gì khác với nó, vì vậy bất cứ nơi nào bên ngoài để cho qua. Bạn sẽ thấy màn hình hiển thị trong hình 9-11. Chú ý rằng lưới dữ liệu cho thấy một hàng tiêu đề cột | CHAPTER 8 INTRODUCING DATASETS AND DATA ADAPTERS 183 set sort string srt companyname asc display filtered and sorted data foreach DataRow row in dtc customers .Select fl srt 0 t 1 row CompanyName .ToString .PadRight 25 row ContactName display data from second data table display output header n----------------------------- Results from Products table ProductName .PadRight 20 UnitPrice .PadLeft 21 n display data foreach DataRow row in dtc 1 .Rows 0 t 1 row productname .ToString .PadRight 25 row unitprice catch Exception e Error e finally close connection 184 CHAPTER 8 INTRODUCING DATASETS AND DATA ADAPTERS 3. Make this the startup project and run it with Ctrl F5. You should see the result shown in Figure 8-4. Figure 8-4. Filtering and sorting a data table How It Works You code and combine two queries for execution on the same connection query 1 string sqll @ select from customers query 2 string sql2 @ select from products where unitprice 10 CHAPTER 8 INTRODUCING DATASETS AND DATA ADAPTERS 185 combine queries string sql sqll sql2 create connection SqlConnection conn new SqlConnection connString You create a data adapter assigning to its SelectCommand property a command that encapsulates the query and connection for internal use by the data adapter s Fill method create data adapter SqlDataAdapter da new SqlDataAdapter new SqlCommand sql conn You then create and fill a dataset create and fill dataset DataSet ds new DataSet ds customers Each query returns a separate result set and each result set is stored in a separate data table in the order in which the queries were specified . The first table is explicitly named customers the second is given the default name customers1. You get the data table collection from the dataset Tables property for ease of reference later get the data tables collection DataTableCollection dtc .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.