** Introduction to DataSets and working with XML files **
** By Alexandru Savescu **
数据集与 XML 文件的使用介绍
This article gives you an introduction to .NET's DataSets and how you can use them with XML files
这篇文章介绍了 .NET 数据集并教会你如何把它们跟 XML 文件结合起来使用
** Introduction **
介绍
This article gives you an introduction to using DataSets and how to use them with XML files. Working with them really makes your life easier when you want to share data from a data source and you are thinking of XML.
这篇文章介绍了 .NET 数据集并教会你如何把它们跟 XML 文件结合起来使用。当你想从数据源中共享数据而且想到使用 XML 的话,这将是很轻松的事情。
** System Requirements **
系统要求
To compile the solution you need to have Microsoft Visual Studio .NET installed. To run any of the client executable you need to have the .NET framework installed.
为了编译程序,你必须安装微软 Visual Studio .NET 。同时为了客户端程序的执行, .NET 框架也是不可缺少的。
The sample provided is a simple application written in C#. It displays a form with a DataGrid . By default, the application connects to a SQL Server, binds the Northwind database and fetches some parent records from table Customers and some child records from table Orders . By default, the application assumes that you have an instance of SQL Server installed on your machine. If this is not the case, then you must manually modify the connection string and then rebuild the sample.
这里提供的是一个用 C# 编写的小程序。它用了一个 DataGrid 来显示数据内容。默认情况下,程序链接到 SQL Server 服务器,帮定 Northwind 数据库,从 Customers 表取出主表数据,从 Orders 表取从表数据。这里假定你已经安装了 SQL Server 数据库服务器。如果不这样的话,你必须手动修改连接字符串并重新建立。
Then, you can save the dataset to XML file. Schema information is also saved.
然后你就可以把数据集保存到 XML 文件了,架构信息也会被保存的。
** What is a DataSet? **
什么是数据集?
A DataSet object is a very generic object that can store cached data from a database in a very efficient way. It is member of the System::Data namespace.
一个数据集对象是一个能够以高效方来存储来自数据库高速缓冲数据的通用对象。它是 System::Data 命名空间的一个成员。
One obvious question is: When to use a dataset? Well, the answer is: it depends. You should consider that a dataset is a collection of in-memory cached data. So it's good to use datasets when:
一个明显的问题是:什么时候使用数据集呢?答案是这样的:它取决于。你应该明白数据集存储的是内存缓冲器里的数据。所以这几个情况下使用数据集是最好的:
- You are working with multiple separated tables or tables from different data sources.
- You are exchanging data with another application such as a Web Service.
- You perform extensive processing with the records in the database. If you use a SQL query every time you need to change something, processing each record may result in connection being held open which may affect performance.
- You want to perform XML/XSLT operations on the data.
l 你同时使用多个独立的数据表或数据表来自不同的数据源。
l 你要作类似于 Web Service 这样的数据交换。
l 你要处理数据库重大量的记录。如果你每次都使用一条 SQL 查询来操作数据库,这样将影响性能。
l 你想使用 XML/XSLT 对数据进行操作。
You should not use a dataset if:
如果在这种情况下,你就应该使用数据集:
- You are using Web Forms in your application because Web Forms and their controls are recreated each time a page is requested by the client. Thus, creating, filling and destroying a dataset each time will be inefficient unless you plan to cache it between roundtrips.
你在程序使用 Web 窗体,而 Web 窗体和它们的控件每次在页面生成的时候都会在客户端重新建立。这样,你要是不打算把数据集放在缓冲里,每次建立、填充和销毁数据集都会影响效率。
A DataSet has a DataTableCollection object as a member that is nothing but a collection of DataTable objects. You declare a DataSet object and add tables to it like this (in Managed C++):
数据集有存放数据表集合的 DataTableCollection 对象。你可以声明一个数据集对象并像下面这样把表添加进去。
_ // Declare the DataSet object _
DataSet* MyDataSet = new DataSet ( "MyDataSet" ); _ // give it a name here _
_ // Add two tables to it _
_ // - add a Table named Table1 _
DataTable* Table1 = MyDataSet->Tables->Add ( "Table1" );
_ // - add a Table named Table2 _
DataTable* Table2 = MyDataSet->Tables->Add ( "Table2" );
You can refer the tables later either using the pointers returned by the Add method or like this:
也可以用 Add 方法或这样返回的结果来引用数据表:
DataTable* table = MyDataSet->Tables->Item[ 0 ]; _ // or _
DataTable* table = MyDataSet->Tables->Item[ "Table1" ];
_ // isn't this indexation cool? _
A DataTable object has two important members: Rows and Columns . Rows is a DataRowCollection object and Columns is a DataColumnCollection . DataRowCollection is a collection of DataRow objects and DataColumnCollection is a collection of DataColumn objects. I am sure you can easily figure out what these objects represent. :)
数据表对象有两个重要成员:行和列( Rows/Columns )。 Rows 是一个 DataRowCollection 对象,而 Columns 是一个 DataColumnColletion 对象。 DataRowCollection 与 DataColunmCollection 分别是 DataRow 与 DataColumn 对象集合。相信你能够轻松的理解这些对象的含义。 J
Adding data to a data set is straight-forward:
单向填充数据
_ // adding data to the first table in the DataSet _
DataTable* Table1 = MyDataSet->Tables->Item[ 0 ];
_ // add two columns to the table _
Table1->Columns->Add ( "Column1" );
Table2->Columns->Add ( "Column2" );
_ // get the collection of rows _
DataRowCollection* drc = Table1->Rows;
_ // create a vector of Objects that we will insert in current row _
Object* obj[] = new Object* [ 2 ];
obj[ 0 ] = new String ( "Item 1" );
obj[ 1 ] = new String ( "Item 2" );
_ // add them to the dataset _
drc->Add (obj);
If you want to specify the data type of a particular column you should use the DataColumn::DataType property. You can set any of the following data types: Boolean , Byte , Char , DateTime , Decimal , Double , Int16 , Int32 , Int64 , SByte , Single , String , TimeSpan , UInt16 , UInt32 , UInt64 .
如果你想指定某列的数据类型,可以使用 DataColumn::DataType 。你也可以设置下面这些数据类型: Boolean 、 Byte 、 Char 、 DataTime 、 Decimal 、 Double 、 Int16 、 Int32 、 Int64 、 SByte 、 Single 、 String 、 TimeSpan 、 UInt16 、 UInt32 、 UInt64 。
That's it! Well, that's all you have to do if you want to build up your data set manually. This is not how it is done if you want to connect to a real data source.
就这些内容了,如果你想手动建立数据集,这就是你需要知道的。如果你要连到一个真正的数据源的话,就不是这样了。
** Binding a Database **
帮定一个数据库
To connect to a database server (such as SQL Server) and fill a dataset from there you need three additonal objects: a Connection , a DataCommand and a DataAdapter object.
要连接到数据库服务器(例如 SQL Server )并要填充数据集,你需要三个额外的对象: Connection 、 DataCommand 、 DataAdapter 。
The Connection object is used to connect to the database object. You must provide a connection string to it. Also, if your application is using transactions, you must attach a transaction object to the connection.
Connection 用于连接数据库对象。你必须为它提供一个连接字符串。如果你的程序使用事务的话,你也得把事务对象附加给连接。
The DataCommand object is used to sending commands to the database server. It includes four System::String objects named SelectCommand , InsertCommand , UpdateCommand and DeleteCommand . I believe it is obvious what these string objects represent, nothing else but the four basic SQL operations.
DataCommand 对象用于给数据库服务器发送命令。它包含四个 System::String 类型的对象: SelectCommand 、 InsertCommand 、 UpdateCommand 、 <SPAN lang=EN-