1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 将批注用于类型化 DataSet (摘自MSDN)

将批注用于类型化 DataSet (摘自MSDN)

时间:2021-10-22 21:45:48

相关推荐

将批注用于类型化 DataSet (摘自MSDN)

批注使您能够在不修改基础架构的情况下修改类型化DataSet中元素的名称。如果修改基础架构中元素的名称,则会使类型化DataSet引用不存在于数据源中的对象,并且会丢失对存在于数据源中的对象的引用。

利用批注,您可以使用更有意义的名称来自定义类型化DataSet中对象的名称,从而使代码更易于阅读,类型化DataSet更易于为客户端使用,同时保持基础架构不变。例如,Northwind数据库中Customers表的以下架构元素会生成CustomersRow这一DataRow对象名称和一个名为Customers的DataRowCollection。

<xs:element name="Customers"><xs:complexType><xs:sequence><xs:element name="CustomerID" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element>

DataRowCollection名称Customers在客户端代码中是有意义的,但DataRow名称CustomersRow则会导致误解,因为它是单个对象。此外,在通常情况下,将不使用Row标识符来引用该对象,而仅将该对象当作Customer对象来引用。解决方案是批注架构并标识DataRow和DataRowCollection对象的新名称。下面是上一架构的批注版本。

<xs:element name="Customers"codegen:typedName="Customer" codegen:typedPlural="Customers"><xs:complexType><xs:sequence><xs:element name="CustomerID" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element>

将Customer的值指定为typedName将生成DataRow对象名称Customer。将Customers的值指定为typedPlural则会保留DataRowCollection名称Customers。

下表显示可用的批注。

下表显示可为nullValue批注指定的值。

下表显示类型化DataSet中对象的默认值以及可用的批注。

若要使用类型化DataSet批注,则必须在 XML 架构定义语言 (XSD) 架构中包含以下xmlns引用。

xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"

下面是一个批注架构示例,它公开Northwind数据库的Customers表并包含与Orders表的关系。

<?xml version="1.0" encoding="utf-8"?><xs:schema id="CustomerDataSet"xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"xmlns="" xmlns:xs="/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="CustomerDataSet" msdata:IsDataSet="true"><xs:complexType><xs:choice maxOccurs="unbounded"><xs:element name="Customers"codegen:typedName="Customer" codegen:typedPlural="Customers"><xs:complexType><xs:sequence><xs:element name="CustomerID"codegen:typedName="CustomerID"type="xs:string" minOccurs="0" /><xs:element name="CompanyName"codegen:typedName="CompanyName"type="xs:string" minOccurs="0" /><xs:element name="Phone"codegen:typedName="Phone" codegen:nullValue=""type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element><xs:element name="Orders"codegen:typedName="Order" codegen:typedPlural="Orders"><xs:complexType><xs:sequence><xs:element name="OrderID"codegen:typedName="OrderID"type="xs:int" minOccurs="0" /><xs:element name="CustomerID"codegen:typedName="CustomerID" codegen:nullValue=""type="xs:string" minOccurs="0" /><xs:element name="EmployeeID"codegen:typedName="EmployeeID" codegen:nullValue="0"type="xs:int" minOccurs="0" /><xs:element name="OrderDate"codegen:typedName="OrderDate" codegen:nullValue="1980-01-01T00:00:00"type="xs:dateTime" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType><xs:unique name="Constraint1"><xs:selector xpath=".//Customers" /><xs:field xpath="CustomerID" /></xs:unique><xs:keyref name="CustOrders" refer="Constraint1"codegen:typedParent="Customer" codegen:typedChildren="GetOrders"><xs:selector xpath=".//Orders" /><xs:field xpath="CustomerID" /></xs:keyref></xs:element></xs:schema>

以下代码示例使用从示例架构创建的强类型DataSet。它使用一个DataAdapter填充Customers表,并使用另一个DataAdapter填充Orders表。强类型DataSet定义DataRelations。

[VisualBasic]Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _"Initial Catalog=northwind")Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName, Phone FROM Customers", &nwindConn)Dim orderDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders", &nwindConn)' Populate a strongly typed DataSet.nwindConn.Open()Dim custDS As CustomerDataSet = New CustomerDataSet()custDA.Fill(custDS, "Customers")orderDA.Fill(custDS, "Orders")nwindConn.Close()' Add a strongly typed event.AddHandler custDS.Customers.CustomerChanged, &New CustomerDataSet.CustomerChangeEventHandler(AddressOf OnCustomerChanged)' Add a strongly typed DataRow.Dim newCust As CustomerDataSet.Customer = custDS.Customers.NewCustomer()newCust.CustomerID = "NEW01"panyName = "My New Company"custDS.Customers.AddCustomer(newCust)' Navigate the child relation.Dim customer As CustomerDataSet.CustomerDim order As CustomerDataSet.OrderFor Each customer In custDS.CustomersConsole.WriteLine(customer.CustomerID)For Each order In customer.GetOrders()Console.WriteLine(vbTab & order.OrderID)NextNextPrivate Shared Sub OnCustomerChanged(sender As Object, e As CustomerDataSet.CustomerChangeEvent)End Sub

[C#]SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName, Phone FROM Customers", nwindConn);SqlDataAdapter orderDA = new SqlDataAdapter("SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders", nwindConn);// Populate a strongly typed DataSet.nwindConn.Open();CustomerDataSet custDS = new CustomerDataSet();custDA.Fill(custDS, "Customers");orderDA.Fill(custDS, "Orders");nwindConn.Close();// Add a strongly typed event.custDS.Customers.CustomerChanged += new CustomerDataSet.CustomerChangeEventHandler(OnCustomerChanged);// Add a strongly typed DataRow.CustomerDataSet.Customer newCust = custDS.Customers.NewCustomer();newCust.CustomerID = "NEW01";panyName = "My New Company";custDS.Customers.AddCustomer(newCust);// Navigate the child relation.foreach(CustomerDataSet.Customer customer in custDS.Customers){Console.WriteLine(customer.CustomerID);foreach(CustomerDataSet.Order order in customer.GetOrders())Console.WriteLine("\t" + order.OrderID);}protected static void OnCustomerChanged(object sender, CustomerDataSet.CustomerChangeEvent e){}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。