ASP. NET 3.5 core programming learning notes (15): Dataset

Source: Internet
Author: User

The dataset class is the main component of the ADO. Net object model. The ADO. Net container class has nothing to do with the data source used and does not store any information related to the specific data source.

DataSet object

The DataSet object implements three important interfaces:

1.
Ilistsource enables it to return the list of data that can be bound to an element.

2. iseralizable enables it to control the way data is serialized, so that it can be passed to the. NET formatter.

3. ixmlserializable enables it to serialize itself into XML.

The following table lists the properties of the dataset class:

 

The attributes of namespace and prefix affect the way dataset serializes itself into XML. The dataset name is used to set the root node of the XML document. If datasetname is null, newdataset is the default name. The methods for this class are shown in the following table:

 

Dataset Data Reading

Dataset and data readers are both ways for ADO. Net Applications to read data, but basically, ADO. Net only has one physical method to read data-using a data reader. Dataset automatically fills in data through the reader.

You can use the createdatareader method of dataset to create a data reader to read data. This method returns a datatablereader object for table traversal in memory. As follows:

DataSet data = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(“Select * From employees;Select * From pruducts”, connString);

adapter.Fill(data);

dataTableReader reader = data.CreateDataReader();
do
{
while(reader.Read())
{
Response.Write(String.Format(“{0} <br />”, reader[1]));
}
Response.Write(“}while(reader.NextResult());

reader.Close();

DataSet object Merging

The merge method can combine two dataset objects. The modes of these two objects should be basically the same, but they are not strictly the same.

The merge operation first compares the modes of the two dataset objects and checks whether they match. If the imported table contains a new column or a new table source, the result depends on the setting of "missing mode operation. By default, the missing mode element is added to the dataset, but this behavior can be changed through another duplicate version of merge (accepting one missingschemaaction parameter.

A Merge operation is an atomic operation that ensures integrity and consistency. Therefore, constraints are disabled during the merge operation. However, if the original constraints cannot be restored after merging, an exception is thrown, but the source data is not lost. In this case, the merge method completely disables the constraint in dataset. It sets the enforeconstraints attribute to false and marks all invalid data as an error. To restore constraints, you must first handle those errors.

Dataset submission Model

When dataset is loaded for the first time, all the records in the table are marked as "unchanged", and the row status is stored in the rowstate attribute. The valid values of the row state are defined in the datarowstate enumeration. See the table below:

 

Each change operation performed on a dataset Member changes the status of the relevant row. Before using the acceptchanges method, all changes will be suspended and remain uncommitted. The acceptchanges method is used to submit all changes and copy the current value to the original value. After the acceptchanges API is called, all changes are cleared. The changed values are integrated into the corresponding rows and displayed as unchanged. Rejectchanges is used to roll back all changes with errors suspended during the change process and restore the original value.

XML serialization of data

There are two methods to serialize the content of a DataSet object to XML: stateless serialization and stateful serialization.

Stateless serialization uses the getxml method or writexml method to take a snapshot of the current data instance (that is, to save only the current data) and generate a snapshot based on the specific XML mode (ADO. Net General paradigm.

Stateful serialization refers to the serialization result that contains historical data information and information related to changes and suspension errors. The following table summarizes the set-in options. The available modes are defined in the writexmlmode enumeration:

 

Ingoreschema is the default option. The following code serializes dataset into an XML file:

Dataset. writexml ("C: \ TMP. xml ");

Serialization and Remote Format

In addition to XML serialization, dataset also supports. net binary serialization. Dataset implements the iserializable interface to fully control the serialization process. The remotingformat attribute of dataset is used to specify the serialization format as XML (default) or binary. Sample Code:

DataSet ds = GetData();
ds.RemotingFormat = SerializationFormat.Binary;
StreamWriter writer = new StreamWriter(BinFile);
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(writer.BaseStream, ds);
writer.Close();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.