Data Set

DataSet is a collection of DataTables. We use the DataSet type to store many DataTables in a single collection. Conceptually, the DataSet acts as a set of DataTable instances. This simplifies programs that use many DataTables.

DataTable

Create

Note

To effectively use the DataSet, you will need to have some DataTables handy. In this program, we create two DataTables. One stores two rows of patient information. And the second stores two rows of medication information.

Next:We create a DataSet with the DataSet constructor.
Then we add the two DataTables to the DataSet instance.
Finally we print the XML.

XML
Program that uses DataSet: C#

using System;
using System.Data;

class Program
{
    static void Main()
    {
	// Create two DataTable instances.
	DataTable table1 = new DataTable("patients");
	table1.Columns.Add("name");
	table1.Columns.Add("id");
	table1.Rows.Add("sam", 1);
	table1.Rows.Add("mark", 2);

	DataTable table2 = new DataTable("medications");
	table2.Columns.Add("id");
	table2.Columns.Add("medication");
	table2.Rows.Add(1, "atenolol");
	table2.Rows.Add(2, "amoxicillin");

	// Create a DataSet and put both tables in it.
	DataSet set = new DataSet("office");
	set.Tables.Add(table1);
	set.Tables.Add(table2);

	// Visualize DataSet.
	Console.WriteLine(set.GetXml());
    }
}

Output

<office>
  <patients>
    <name>sam</name>
    <id>1</id>
  </patients>
  <patients>
    <name>mark</name>
    <id>2</id>
  </patients>
  <medications>
    <id>1</id>
    <medication>atenolol</medication>
  </medications>
  <medications>
    <id>2</id>
    <medication>amoxicillin</medication>
  </medications>
</office>

Dispose, using DataSet

Using keyword

You can put your DataSet in a using block. This ensures the Dispose method is called as soon as possible when the DataSet is no longer being used. If you are having resource usage problems, adding using blocks can help.

Using
Program that creates DataSet in using block: C#

using System.Data;

class Program
{
    static void Main()
    {
	// Create a DataSet in using statement.
	using (DataSet set = new DataSet("office"))
	{
	    // Put code that adds stuff to DataSet here.
	    // ... The DataSet will be cleaned up outside the block.
	}
    }
}

Namespace, Prefix

One important use of the DataSet is to encode data in XML format. Often, XML data needs to have an XML namespace of a tag element prefix. Fortunately, the DataSet provides the Namespace and Prefix properties to specify this.

Next:This example specifies both the Namespace and the Prefix, and you can see them appear in the output.

Program that uses Namespace and Prefix: C#

using System;
using System.Data;

class Program
{
    static void Main()
    {
	DataTable table1 = new DataTable("patients");
	table1.Columns.Add("name");
	table1.Columns.Add("id");
	table1.Rows.Add("sam", 1);

	// Create a DataSet.
	DataSet set = new DataSet("office");
	set.Tables.Add(table1);
	set.Namespace = "y";
	set.Prefix = "x";

	// Visualize DataSet.
	Console.WriteLine(set.GetXml());
    }
}

Output

<x:office xmlns:x="y">
  <patients xmlns="y">
    <name>sam</name>
    <id>1</id>
  </patients>
</x:office>

DataSetName

Every DataSet can have a name specified. Usually, it is easiest to specify this inside the DataSet constructor. However, you can also change the name by assigning to the DataSetName property. You can also read the DataSetName property.

Program that uses DataSetName: C#

using System;
using System.Data;

class Program
{
    static void Main()
    {
	// Create a DataSet.
	DataSet set = new DataSet("office");

	// Show original name.
	Console.WriteLine(set.DataSetName);

	// Change its name.
	set.DataSetName = "unknown";
	Console.WriteLine(set.DataSetName);
    }
}

Output

office
unknown

Copy, Clear

Framework: NET

The DataSet is similar to other popular collections. For example, it has a Clear method that clears all the DataTables in the set. It also provides a Copy method that will make a deep copy of all the DataTables in the set.

Tip:If you call Copy and the Clear the original, your copied data will still exist unchanged.

Program that uses Copy and Clear: C#

using System;
using System.Data;

class Program
{
    static void Main()
    {
	DataTable table1 = new DataTable("patients");
	table1.Columns.Add("name");
	table1.Columns.Add("id");
	table1.Rows.Add("sam", 1);

	DataTable table2 = new DataTable("medications");
	table2.Columns.Add("id");
	table2.Columns.Add("medication");
	table2.Rows.Add(1, "atenolol");

	// Create a DataSet.
	DataSet set = new DataSet("office");
	set.Tables.Add(table1);
	set.Tables.Add(table2);

	// Copy the DataSet.
	DataSet copy = set.Copy();

	// Clear the first DataSet.
	set.Clear();

	// Show contents.
	Console.WriteLine("set: {0}", set.GetXml());
	Console.WriteLine("copy: {0}", copy.GetXml());
    }
}

Output

set: <office />
copy: <office>
  <patients>
    <name>sam</name>
    <id>1</id>
  </patients>
  <medications>
    <id>1</id>
    <medication>atenolol</medication>
  </medications>
</office>

Tables

Understanding the Tables collection in the DataSet is important. The Tables property returns an instance of a DataTableCollection. You can use the Count property and indexer on this sub-collection to access all the individual tables.

Indexer
Program that uses Tables and DataTableCollection: C#

using System;
using System.Data;

class Program
{
    static void Main()
    {
	DataTable table1 = new DataTable("patients");
	table1.Columns.Add("name");
	table1.Columns.Add("id");
	table1.Rows.Add("sam", 1);

	DataTable table2 = new DataTable("medications");
	table2.Columns.Add("id");
	table2.Columns.Add("medication");
	table2.Rows.Add(1, "atenolol");
	table2.Rows.Add(6, "trifluoperazine");

	// Create a DataSet.
	DataSet set = new DataSet("office");
	set.Tables.Add(table1);
	set.Tables.Add(table2);

	// Loop over DataTables in DataSet.
	DataTableCollection collection = set.Tables;
	for (int i = 0; i < collection.Count; i++)
	{
	    DataTable table = collection[i];
	    Console.WriteLine("{0}: {1}", i, table.TableName);
	}

	// Write name of first table.
	Console.WriteLine("x: {0}", set.Tables[0].TableName);

	// Write row count of medications table.
	Console.WriteLine("y: {0}", set.Tables["medications"].Rows.Count);
    }
}

Output

0: patients
1: medications
x: patients
y: 2

Also, you can get a DataTable from the Tables collection with its name. The last part of the program where we use set.Tables["medications"] shows this behavior. This is an intuitive way of getting a certain table.

Relations

Property

The Relations property is a way to specify many DataRelations on the DataTables. A DataRelation indicates which tables are dependent on other tables (sub-tables). The Relations property is not covered in this document.

CaseSensitive

The DataSet provides the CaseSensitive property. The default value of this property is False. There may be cases where you want string lookups on your DataSet to be case-sensitive.

Note:One case involves two elements with the same name but different character casing ("Medications", "medications").

GetXml

Extensible markup language: XML

It is possible to convert a DataSet to a string representation in XML syntax. The GetXml() method on the DataSet instance is ideal for this. The example program constructs a new DataSet instance with the name "Hospital".

Then:It adds a new DataTable to this set.
This DataTable has four rows and five columns.

Finally:The GetXml instance method is invoked on the DataSet, and the result is printed to the screen.

Program that uses GetXml method: C#

using System;
using System.Data;

class Program
{
    static DataTable Table()
    {
	DataTable table = new DataTable("Prescription");
	table.Columns.Add("Dosage", typeof(int));
	table.Columns.Add("Drug", typeof(string));
	table.Columns.Add("Patient", typeof(string));
	table.Columns.Add("Date", typeof(DateTime));

	table.Rows.Add(25, "Indocin", "David", DateTime.Now);
	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
	table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
	table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
	table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
	return table;
    }

    static void Main()
    {
	// Create DataSet instance.
	DataSet set = new DataSet("Hospital");
	// Add new table.
	set.Tables.Add(Table());
	// Write xml data.
	Console.WriteLine(set.GetXml());
    }
}

Output

<Hospital>
  <Prescription>
    <Dosage>25</Dosage>
    <Drug>Indocin</Drug>
    <Patient>David</Patient>
    <Date>2010-06-17T08:39:41.0879713-06:00</Date>
  </Prescription>
  <Prescription>
    <Dosage>50</Dosage>
    <Drug>Enebrel</Drug>
    <Patient>Sam</Patient>
    <Date>2010-06-17T08:39:41.0879713-06:00</Date>
  </Prescription>
  <Prescription>
    <Dosage>10</Dosage>
    <Drug>Hydralazine</Drug>
    <Patient>Christoff</Patient>
    <Date>2010-06-17T08:39:41.0879713-06:00</Date>
  </Prescription>
  <Prescription>
    <Dosage>21</Dosage>
    <Drug>Combivent</Drug>
    <Patient>Janet</Patient>
    <Date>2010-06-17T08:39:41.0879713-06:00</Date>
  </Prescription>
  <Prescription>
    <Dosage>100</Dosage>
    <Drug>Dilantin</Drug>
    <Patient>Melanie</Patient>
    <Date>2010-06-17T08:39:41.0879713-06:00</Date>
  </Prescription>
</Hospital>

In the XML file, the element names Hospital, Prescription, Dosage, Drug, Patient and Date correspond to the name of the DataSet, the name of the DataTable, and then the four different DataColumns. The data is preserved.

GetXmlSchema

An XML schema is a text document that indicates the structure of an XML document. The GetXmlSchema() method on the DataSet type generates an XML schema from the known structure encoded in your DataSet.

Then:We create a DataSet and then add a DataTable instance to it. We call the GetXmlSchema instance method, which reveals the XML schema.

Program that demonstrates GetXmlSchema: C#

using System;
using System.Data;

class Program
{
    static DataTable Table()
    {
	DataTable table = new DataTable("Prescription");
	table.Columns.Add("Dosage", typeof(int));
	table.Columns.Add("Drug", typeof(string));
	table.Columns.Add("Patient", typeof(string));
	table.Columns.Add("Date", typeof(DateTime));

	table.Rows.Add(25, "Indocin", "David", DateTime.Now);
	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
	table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
	table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
	table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
	return table;
    }

    static void Main()
    {
	// Create DataSet instance.
	DataSet set = new DataSet("Hospital");
	// Add new table.
	set.Tables.Add(Table());
	// Write xml schema data.
	Console.WriteLine(set.GetXmlSchema());
    }
}

Output: edited

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="Hospital" xmlns="" xmlns:xs="" xmlns:msdata="">
  <xs:element name="Hospital" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
	<xs:element name="Prescription">
	  <xs:complexType>
	    <xs:sequence>
	      <xs:element name="Dosage" type="xs:int" minOccurs="0" />
	      <xs:element name="Drug" type="xs:string" minOccurs="0" />
	      <xs:element name="Patient" type="xs:string" minOccurs="0" />
	      <xs:element name="Date" type="xs:dateTime" minOccurs="0" />
	    </xs:sequence>
	  </xs:complexType>
	</xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
Question and answer

Using XML schema with DataSet. Now that we have an XML schema file, what can we do with it? The DataSet type provides two methods, InferXmlSchema and ReadXmlSchema, which can load a file we specify that contains the schema data.

Then:We have a DataSet with all the appropriate constraints in it. After this, we could use ReadXml on an XML file of the data itself.

Summary

The DataSet type provides a way to collect many DataTables inside a single collection. It provides useful helper methods for acting upon those DataTables. By providing a container for DataTables, DataSet introduces a key abstraction.

Mill data set指的是一个关于纺织厂的数据集。这个数据集包含了与纺织厂相关的各种信息和指标,用于分析和研究纺织工业的运营和生产情况。 Mill data set通常包含以下信息:纺织厂的位置、规模和产能、生产设备和工艺、员工人数和组织结构、原料和产品类型、生产效率和质量指标、能源和资源消耗等。这些数据可用于分析纺织厂的生产能力、生产效率、成本和质量控制等方面的情况。 借助Mill data set,我们可以进行各种分析和研究。首先,可以通过比较不同纺织厂的数据,寻找最佳实践和关键成功因素,以改善纺织厂的运作。其次,可以使用数据集中的指标来评估纺织厂的运营绩效,并与行业标准进行比较,以确定其竞争力和改进空间。此外,数据集还可以用于预测和优化纺织生产过程,提高生产效率和产品质量。 Mill data set对纺织行业的管理决策和战略制定具有重要意义。通过对纺织厂各方面数据的深入分析,可以识别出问题和挑战,并提出相应的解决方案。同时,数据集还可以帮助纺织企业制定长期发展计划和投资决策,以适应市场需求和技术变革。 综上所述,Mill data set是一个关于纺织厂的数据集,包含了各种与纺织厂运营和生产相关的信息和指标。通过对这些数据的分析和研究,可以改善纺织厂的运作,提高生产效率和质量,并为管理决策和战略制定提供重要依据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值