创建一个简单的数据集
一开始,我们要创建一个简单的用于描述一个电话本的数据集,并填上一些简单的示范数据。接着从一个Web服务中返回这个数据集,注意数据集是如何串行化到XML中的。
[WebMethod] public DataSet getDataSet()
{
// Create a new data set and add a table to it.
DataSet dataSet = new DataSet("MyDataSet");
DataTable table = dataSet.Tables.Add("PhoneBook");
// Create the columns of the table.
DataColumn primaryKeyCol = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Telephone", typeof(string));
// Set the ID field as the primary key.
table.PrimaryKey = new DataColumn[] { primaryKeyCol };
// Create some bogus data.
table.LoadDataRow(new object[] {1, "Fred", "555.2145"}, true);
table.LoadDataRow(new object[] {2, "Bob", "555.6246"}, true);
table.LoadDataRow(new object[] {3, "Howard", "555.1125"}, true);
table.LoadDataRow(new object[] {4, "Stanley", "555.0932"}, true);
// Return the data set to the caller.
return dataSet;
}
{
// Create a new data set and add a table to it.
DataSet dataSet = new DataSet("MyDataSet");
DataTable table = dataSet.Tables.Add("PhoneBook");
// Create the columns of the table.
DataColumn primaryKeyCol = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Telephone", typeof(string));
// Set the ID field as the primary key.
table.PrimaryKey = new DataColumn[] { primaryKeyCol };
// Create some bogus data.
table.LoadDataRow(new object[] {1, "Fred", "555.2145"}, true);
table.LoadDataRow(new object[] {2, "Bob", "555.6246"}, true);
table.LoadDataRow(new object[] {3, "Howard", "555.1125"}, true);
table.LoadDataRow(new object[] {4, "Stanley", "555.0932"}, true);
// Return the data set to the caller.
return dataSet;
}
这个数据集的规划很简单。数据集中只有一个名为“PhoneBook”的表,该表包含了三列。一列为ID,该列将作为关键字;一列为Name,还有一列为Telephone。在数据集中我们已经添加了一些简单的数据。当调用上述方法时,将返回以下XML:
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="MyDataSet"
xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="MyDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="PhoneBook">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" />
<xs:element name="Name" type="xs:string"
minOccurs="0" />
<xs:element name="Telephone" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//PhoneBook" />
<xs:field xpath="ID" />
</xs:unique>
</xs:element>
</xs:schema>
<diffgr:diffgram
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<MyDataSet xmlns="">
<PhoneBook diffgr:id="PhoneBook1" msdata:rowOrder="0">
<ID>1</ID>
<Name>Fred</Name>
<Telephone>555.2145</Telephone>
</PhoneBook>>
<PhoneBook diffgr:id="PhoneBook2" msdata:rowOrder="1">
<ID>21</ID>
<Name>Bob</Name>
<Telephone>555.6246</Telephone>
</PhoneBook>
<PhoneBook diffgr:id="PhoneBook3" msdata:rowOrder="2">
<ID>3</ID>
<Name>Howard</Name>
<Telephone>555.1125</Telephone>
</PhoneBook>
<PhoneBook diffgr:id="PhoneBook4" msdata:rowOrder="3">
<ID>4</ID>
<Name>Stanley</Name>
<Telephone>555.0932</Telephone>
</PhoneBook>
</MyDataSet>
</diffgr:diffgram>
</DataSet>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="MyDataSet"
xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="MyDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="PhoneBook">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" />
<xs:element name="Name" type="xs:string"
minOccurs="0" />
<xs:element name="Telephone" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//PhoneBook" />
<xs:field xpath="ID" />
</xs:unique>
</xs:element>
</xs:schema>
<diffgr:diffgram
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<MyDataSet xmlns="">
<PhoneBook diffgr:id="PhoneBook1" msdata:rowOrder="0">
<ID>1</ID>
<Name>Fred</Name>
<Telephone>555.2145</Telephone>
</PhoneBook>>
<PhoneBook diffgr:id="PhoneBook2" msdata:rowOrder="1">
<ID>21</ID>
<Name>Bob</Name>
<Telephone>555.6246</Telephone>
</PhoneBook>
<PhoneBook diffgr:id="PhoneBook3" msdata:rowOrder="2">
<ID>3</ID>
<Name>Howard</Name>
<Telephone>555.1125</Telephone>
</PhoneBook>
<PhoneBook diffgr:id="PhoneBook4" msdata:rowOrder="3">
<ID>4</ID>
<Name>Stanley</Name>
<Telephone>555.0932</Telephone>
</PhoneBook>
</MyDataSet>
</diffgr:diffgram>
</DataSet>
