强类型数据集与弱类型数据集 [英] Strongly typed datasets vs. weakly typed datasets

查看:109
本文介绍了强类型数据集与弱类型数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在.net强类型数据集是什么意思?任何人都可以有一个明确的和简单的例子解释一下吗?

What is meant by strongly typed datasets in .Net? Can anybody explain with a clear and brief example?

和也,之间有什么区别强类型和弱类型数据集?

And also, what is the difference between strongly typed and weakly typed datasets?

推荐答案

键入的对战非类型化数据集
类型化的DataSet是第一个从基DataSet类派生的数据集,然后从数据集设计,存储使用信息在.xsd文件,生成一个新的强类型DataSet类。从架构信息(表,列等)生成并编译成这个新的数据集类作为一组第一类对象和属性。由于类型化的DataSet从基DataSet类继承,类型化类假定所有的DataSet类的功能,并且可以与需要一个DataSet类的实例作为参数

Typed Versus Untyped Datasets A typed dataset is a dataset that is first derived from the base DataSet class and then uses information from the Dataset Designer, which is stored in an .xsd file, to generate a new strongly-typed dataset class. Information from the schema (tables, columns, and so on) is generated and compiled into this new dataset class as a set of first-class objects and properties. Because a typed dataset inherits from the base DataSet class, the typed class assumes all of the functionality of the DataSet class and can be used with methods that take an instance of a DataSet class as a parameter

这是无类型的数据集,与此相反,不具有相应的内置模式。作为一个强类型DataSet,非类型化数据集包含表,列等等 - 但这些都暴露只是作为收藏品。 (然而,在手动创建的非类型化数据集的表和其它数据元素,可以将数据集的结构导出为使用DataSet的WriteXmlSchema方法的模式。)

An untyped dataset, in contrast, has no corresponding built-in schema. As in a typed dataset, an untyped dataset contains tables, columns, and so on — but those are exposed only as collections. (However, after manually creating the tables and other data elements in an untyped dataset, you can export the dataset's structure as a schema using the dataset's WriteXmlSchema method.)

对比数据在类型访问和非类型化的数据集
类为类型化的DataSet具有这样的属性采取对表和列的实际名称的对象模型。例如,如果你是一个类型化的DataSet时,您可以使用代码引用列,如以下内容:

Contrasting Data Access in Typed and Untyped Datasets The class for a typed dataset has an object model in which its properties take on the actual names of the tables and columns. For example, if you are working with a typed dataset, you can reference a column using code such as the following:

C#VBCopy

// This accesses the CustomerID column in the first row of the Customers table.
string customerIDValue = northwindDataSet.Customers[0].CustomerID;



J#复制

J#Copy

// This accesses the CustomerID column in the first row of the Customers table.
String customerIDValue =
    northwindDataSet.get_Customers().get_Item(0).get_CustomerID();

在此相反,如果您正在使用非类型化数据集工作,等效的代码是:

In contrast, if you are working with an untyped dataset, the equivalent code is:

C#VBCopy

 string customerIDValue = (string)
    dataset1.Tables["Customers"].Rows[0]["CustomerID"];



J#复制

J#Copy

 String customerIDValue = (String)
    dataset1.get_Tables().get_Item("Customers").get_Rows().get_Item(0).get_Item("CustomerID");



类型访问不仅更容易阅读,但通过智能感知在Visual Studio代码编辑器完全支持。除了是容易的工作,为类型化数据集的语法提供在编译时类型检查,极大地减少了错误的数据集中到成员分配值的可能性。如果您在DataSet中更改列的名称,然后编译应用程序,您会收到生成错误。通过双击任务列表中生成错误,你可以直接到该行或引用旧列名的代码行。访问表和列在类型化数据集也稍快一些在运行时因为访问是在编译时确定的,而不是通过在运行时集合。

Typed access is not only easier to read, but is fully supported by IntelliSense in the Visual Studio Code Editor. In addition to being easier to work with, the syntax for the typed dataset provides type checking at compile time, greatly reducing the possibility of errors in assigning values to dataset members. If you change the name of a column in your DataSet and then compile your application, you receive a build error. By double-clicking the build error in the Task List, you can go directly to the line or lines of code that reference the old column name. Access to tables and columns in a typed dataset is also slightly faster at run time because access is determined at compile time, not through collections at run time.

尽管类型化数据集有很多优点,也有各种各样的情况下进行非类型化数据集是非常有用的。最明显的情况是,当没有模式可用于数据集。这可能发生,例如,如果你的应用程序与它返回一个数据集的组件进行交互,但你事先不知道它的结构是什么。同样,有些时候,您正在使用没有一个静态的,可预测的结构化数据的工作;在这种情况下,是不切实际的使用类型的数据集,因为你必须重新生成与数据结构中的每个变化的类型化数据集的类

Even though typed datasets have many advantages, there are a variety of circumstances under which an untyped dataset is useful. The most obvious scenario is when no schema is available for the dataset. This might occur, for example, if your application is interacting with a component that returns a dataset, but you do not know in advance what its structure is. Similarly, there are times when you are working with data that does not have a static, predictable structure; in that case, it is impractical to use a typed dataset, because you would have to regenerate the typed dataset class with each change in the data structure.

更一般地,有是时候你可能动态创建一个数据集,而无需架构提供了很多次。在这种情况下,该数据集是一个简单的方便的结构,可以在其中保持的信息,只要该数据能够在关系的方式来表示。同时,你可以采取的数据集的能力优势,如序列化的信息传递给另一个进程,或者写出一个XML文件的能力。

More generally, there are many times when you might create a dataset dynamically without having a schema available. In that case, the dataset is simply a convenient structure in which you can keep information, as long as the data can be represented in a relational way. At the same time, you can take advantage of the dataset's capabilities, such as the ability to serialize the information to pass to another process, or to write out an XML file.

这篇关于强类型数据集与弱类型数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆