您好,登錄后才能下訂單哦!
這篇文章主要講解了“ADO.NET批注在編程中的意義是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ADO.NET批注在編程中的意義是什么”吧!
ADO.NET批注使您能夠在不修改基礎(chǔ)架構(gòu)的情況下修改類型化 DataSet 中元素的名稱。如果修改基礎(chǔ)架構(gòu)中元素的名稱,則會使類型化 DataSet 引用不存在于數(shù)據(jù)源中的對象,并且會丟失對存在于數(shù)據(jù)源中的對象的引用。
利用批注,您可以使用更有意義的名稱來自定義類型化 DataSet 中對象的名稱,從而使代碼更易于閱讀,類型化 DataSet 更易于為客戶端使用,同時(shí)保持基礎(chǔ)架構(gòu)不變。例如,Northwind 數(shù)據(jù)庫中 Customers 表的以下架構(gòu)元素會生成 CustomersRow 這一 DataRow 對象名稱和一個(gè)名為 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 則會導(dǎo)致誤解,因?yàn)樗菃蝹€(gè)對象。此外,在通常情況下,將不使用 Row 標(biāo)識符來引用該對象,而僅將該對象當(dāng)作 Customer 對象來引用。解決方案是為架構(gòu)添加ADO.NET批注并標(biāo)識 DataRow 和 DataRowCollection 對象的新名稱。下面是上一架構(gòu)的批注版本。
<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>
將 typedName 的值指定為 Customer 將生成 DataRow 對象名稱 Customer。將 typedPlural 的值指定為 Customers 則會保留 DataRowCollection 名稱 Customers。
若要使用類型化 DataSet 批注,則必須在 XML 架構(gòu)定義語言 (XSD) 架構(gòu)中包含以下 xmlns 引用。
xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"
下面是一個(gè)ADO.NET批注架構(gòu)示例,它公開 Northwind 數(shù)據(jù)庫的 Customers 表并包含與 Orders 表的關(guān)系。
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="CustomerDataSet" xmlns:codegen="urn:schemas-microsoft-com:xml-msprop" xmlns="" xmlns:xs="http://www.w3.org/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="OrderAdapter" codegen:typedName="OrderAdapter" 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>
感謝各位的閱讀,以上就是“ADO.NET批注在編程中的意義是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ADO.NET批注在編程中的意義是什么這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。