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

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

<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 对象来引用。解决方案是批注架构并标识 DataRowDataRowCollection 对象的新名称。下面是上一架构的批注版本。

<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

下表显示可用的批注。

批注 说明
typedName 对象的名称。
typedPlural 对象集合的名称。
typedParent 对象在父关系中被引用时的名称。
typedChildren 用于从子关系中返回对象的方法的名称。
nullValue 如果基础值为 DBNull,则为值。有关 nullValue 批注的信息,请参见下表。默认为 _throw

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

nullValue 说明
替换值 指定要返回的值。所返回的值必须匹配该元素的类型。例如,使用 nullValue="0" 可为空整数字段返回 0。
_throw 引发异常。这是默认值。
_null 如果遇到基元类型,则返回空引用或引发异常。
_empty 对于字符串返回 String.Empty;否则,返回从空构造函数创建的对象。如果遇到基元类型,则引发异常。

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

对象/方法/事件 默认值 批注
DataTable TableNameDataTable typedPlural
DataTable 方法 NewTableNameRow

AddTableNameRow

DeleteTableNameRow

typedName
DataRowCollection TableName typedPlural
DataRow TableNameRow typedName
DataColumn DataTable.ColumnNameColumn

DataRow.ColumnName

typedName
Property PropertyName typedName
Child Accessor GetChildTableNameRows typedChildren
Parent Accessor TableNameRow typedParent
DataSet 事件 TableNameRowChangeEvent

TableNameRowChangeEventHandler

typedName

若要使用类型化 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="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="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

[Visual Basic]
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"
newCust.CompanyName = "My New Company"
custDS.Customers.AddCustomer(newCust)
' Navigate the child relation.
Dim customer As CustomerDataSet.Customer
Dim order As CustomerDataSet.Order
For Each customer In custDS.CustomersConsole.WriteLine(customer.CustomerID)For Each order In customer.GetOrders()Console.WriteLine(vbTab & order.OrderID)Next
Next
Private 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";
newCust.CompanyName = "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)
{
}

转载于:https://www.cnblogs.com/asyuras/archive/2006/03/13/348821.html

将批注用于类型化 DataSet (摘自MSDN)相关推荐

  1. [摘自MSDN] ASP.Net2.0学习 [1] 母版页 2 : 创建和使用 ASP.NET 母版页

    演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 本演练阐释如何创建一个母版页和几个内容页.母版页使您可以创建一个页面布局(模板页),然后创建各个页面,这些页面 ...

  2. .NET 时间格式 ----------摘自MSDN

    使用在 DateTimeFormatInfo 的属性中存储的标准或自定义模式设置 DateTime 值的格式. 可以通过设置可写 DateTimeFormatInfo 的关联属性用自定义模式替代标准模 ...

  3. [摘自MSDN] ASP.Net2.0学习 [2] 主题 1 :ASP.NET 主题和外观概述

    ASP.NET 主题和外观概述 主题是属性设置的集合,使用这些设置可以定义页面和控件的外观,然后在某个 Web 应用程中的所有页.整个 Web 应用程序或服务器上的所有 Web 应用程中一致地应用此外 ...

  4. [转]编译器选项(摘自MSDN)及VC项目配置基础

    VC 项目配置基础 (请点击打开) 按类别列出的编译器选项Visual Studio 2010 其他版本 Visual Studio 2008 Visual Studio 2005 下面是一个完整的编 ...

  5. 2 获取对象 IDbDataAdapter 用于填充 DataSet 和更新数据源

    //oracle版 new OracleDataAdapter();//SqlServer版 new SqlDataAdapter();//OleDb版 new OleDbDataAdapter(); ...

  6. [摘自MSDN] ASP.Net2.0学习 [1] 母版页 7 : 母版页和内容页中的事件

    ASP.NET ASP.NET 母版页和内容页中的事件 母版页和内容页都可以包含控件的事件处理程序.对于控件而言,事件是在本地处理的,即内容页中的控件在内容页中引发事件,母版页中的控件在母版页中引发事 ...

  7. c# mysql fill_C#里sqlDataAdapter.fill(DataSet,String)的用法

    第二个参数 String是指定DataSet 里表的名字,例如 sqlDataAdapter.fill(DataSet,"学生表") 指定后,以后就可以这样调用这张表 DataSe ...

  8. 数据库-ADONET-使用强类型DataSet

    使用强类型DataSet对象 使用ADONET访问DataSet内容的方式,与使用ADO和DAO的Recordset对象具有类似的编程格式. l ADONET和VBNET txtCompanyName ...

  9. dbunit测试dao_用于数据库测试的DBUnit,Spring和注释

    dbunit测试dao 如果您曾经尝试用Java编写数据库测试,则可能会遇到DBUnit . DBUnit允许您设置和拆除数据库,以便它包含可针对其编写测试的一致行. 通常,您可以通过编写一个简单的X ...

最新文章

  1. 激活函数sigmoid和激活函数softmax
  2. html5和html的区别是什么(精问)
  3. 003_隐藏和显示效果
  4. 【小白学习PyTorch教程】十一、基于MNIST数据集训练第一个生成性对抗网络
  5. 高手进阶:/etc/profile环境变量配置解析
  6. 随笔:朋友圈扫街图有感(爱情)
  7. Cloud一分钟 |百度遭北京信管局行政处罚;双11计算能力超100万核;腾讯回应高管被抓系谣言...
  8. MongoDB 安装记录
  9. Git基本理论、项目搭建、文件操作以及分支介绍
  10. python的字符串删除操作 有点简单
  11. java count rows_Java统计个人编写的Java文件个数及代码行数
  12. rgba颜色和16进制颜色互相转换
  13. vux2.9版本bug
  14. javaweb时间插件
  15. (KNN)K-近邻算法介绍和 Facebook签到位置预测案例应用
  16. 沁园春·长沙 中法对照
  17. [维修案例] 艾美特电风扇(FS4085R)不能调风量维修
  18. triplets 、triplet Loss和 hard triplets
  19. Linux 文件格式转码工具
  20. Java 无需解压直接读取ZIP压缩包里的文件及内容

热门文章

  1. centOS 7 安装man中文版手册
  2. 网络高可用性解决方案
  3. 一个生成全局唯一Sequence ID的高并发工厂类 (Java)
  4. 为SharePoint 2010创建Application Page
  5. 还原活动目录完全手册
  6. rh9.0虚拟机dmesg启动过程分析(2)
  7. Base62x比Base64的编码速度更快吗?
  8. iptables自定义链增加和删除
  9. Linux集群部署和ipvsadm命令的使用
  10. 《Hadoop集群与安全》一2.1 在Hadoop集群中配置操作系统