Tài liệu Truy Xuất Dữ Liệu Với .NET part 6 - Pdf 92


Các Sơ đồ XML
XML là một đường hào vững chắc bao bọc ADO.NET - thật vậy, các định dạng điều
khiển cho việc truyền dữ liệu hiện tại là XML. Với thời gian chạy .NET, nó có thể mô tả
một DataTable trong một file sơ đồ XML. Hơn thế nữa, bạn có thể định nghĩa một
DataSet, với một số DataTables, một bộ các quan hệ giữa các bảng, bao gồm các chi tiết
và mô t
ả đây đủ của dữ liệu.
Khi bạn có một file XSD, có một công cụ trong thời gian chạy để chuyển sơ đồ này thành
các lớp dữ liệu tương ứng, chẳng hạn như một lớp DataTable ở trên. Trong phần này
chúng ta sẽ bắt đầu với một file XSD đơn giản dùng để mô tả các thông tin tương tự ví dụ
Products ở trên, và sau đó tạo ra một vài tính năng mở rộng. File này là Products.xsd
,
trong thư mục 10_XSD_DataSet
:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
id="Products"
targetNamespace="
elementFormDefault="qualified"
xmlns="
xmlns:mstns="
xmlns:xsd="
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductID" type="xs:int" />
<xs:element name="ProductName" type="xs:string" />
<xs:element name="SupplierID" type="xs:int" minOccurs="0" />
<xs:element name="CategoryID" type="xs:int" minOccurs="0" />

đi môt vài mã không cần thiết chỉ giữ lại những gì quan trọng nhất, và làm một vài thao
tác định dạng lại để vừa với khổ giấy. Để xem kết quả cuối cùng, chạy XSD.EXE trên sơ
đồ Products và xem xet file .cs được tạo ra. Mã ví dụ trong thư mục 10_XSD_DataSet
:
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.0.3512.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by xsd, Version=1.0.3512.0.
//
using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;

[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Diagnostics.DebuggerStepThrough()]
[System.ComponentModel.ToolboxItem(true)]
public class Products : DataSet
{
private ProductDataTable tableProduct;
public Products()

private DataColumn columnQuantityPerUnit;
private DataColumn columnUnitPrice;
private DataColumn columnUnitsInStock;
private DataColumn columnUnitsOnOrder;
private DataColumn columnReorderLevel;
private DataColumn columnDiscontinued;

internal ProductDataTable() : base("Product")
{
this.InitClass();
}
Lớp ProductDataTable, được xuất phát từ DataTable và thực thi giao diện IEnumerable,
định nghĩa một thể hiện DataColumn tĩnh cho mỗi cột trong bảng. Chúng được khởi động
một lần nữa từ cấu tử bởi việc gọi phương thức tĩnh InitClass(). Mỗi cột cung cấp một
con trỏ nội, lớp DataRow được mô tả sau.
[System.ComponentModel.Browsable(false)]
public int Count
{
get { return this.Rows.Count; }
}
internal DataColumn ProductIDColumn
{
get { return this.columnProductID; }
}
// Other row accessors removed for clarity - there is one for each of the columns
Việc thêm các dòng vào bảng được cung cấp bởi hai quá tải trong phương thức
AddProductRow(). Quá tải thứ nhất tạo DataRow và không trả về giá trị nào cả. Quá tải
thứ hai là một tập các giá trị, mỗi giá trị dành cho các cột trong DataTable, tạo các giá trị
cho một dòng mới, thêm dòng vào DataTable và trả dòng về cho trình gọi.
public void AddProductRow(ProductRow row)

hợp columns.
private void InitClass()
{
this.columnProductID = new DataColumn ( "ProductID",
typeof(int),
null,
System.Data.MappingType.Element);
this.Columns.Add(this.columnProductID);
// Other columns removed for clarity

this.columnProductID.AutoIncrement = true;


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