ASP.net 2.0 in the DataTable small Mutiny

Source: Internet
Author: User

Let's look at the code for a Web service first.

[WebMethod]
public DataTable GetInfo()
...{
OleDbConnection nwindConn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\Northwind\\northwind.mdb;");
OleDbCommand selectCMD =
new OleDbCommand("SELECT CustomerID, CompanyName FROM Customers"
, nwindConn);
selectCMD.CommandTimeout = 30;
OleDbDataAdapter custDA = new OleDbDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
return custDS.Tables[0];
}

In. NET 1.1, this is a typical error, in. NET 1.1, 1.0, the return or input parameters of a WEB Service cannot be a DataTable, which is a well-known point of knowledge. The reason is that the DataTable does not support serialization as the DataSet does. In. NET 1.1, the way we solve this problem is to use a dataset. But when using a dataset, there is often a sense of overkill.

Attached:. NET 1.1 The use of a DataTable as a Web Service return value is reported as the following exception:

The member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite is an interface and cannot be serialized.

In. NET 2.0, there is no problem with the same code. The reason is that the DataTable in 2.0 implements the serialization and reverse sequence.

In the VS2005 Beta2 documentation, we can see that the 2.0 DataTable implements the following interfaces:

Explicit Interface Implementations
System.ComponentModel.IListSource.get_ContainsListCollection
System.ComponentModel.IListSource.GetList
System.Xml.Serialization.IXmlSerializable.GetSchema
System.Xml.Serialization.IXmlSerializable.ReadXml
System.Xml.Serialization.IXmlSerializable.WriteXml

In 1.1, the DataTable implements only one interface:

Explicit Interface Implementations
System.ComponentModel.IListSource.ContainsListCollection

Some of the features in the dataset are moved into the DataTable, and the merge method in 2.0 is the merging of several datasets.

The code for the DataTable is merged into the following code.

private static void Demonstratemergetable ()
... {
DataTable table1 = new DataTable ("Items");
DataColumn column1 = new DataColumn ("id", typeof (System.Int32));
DataColumn column2 = new DataColumn ("Item", typeof (System.Int32));
Table1. Columns.Add (Column1);
Table1. Columns.Add (Column2);
Table1. PrimaryKey = new datacolumn[] ... {Column1};
Table1. RowChanged + = new System.Data.DataRowChangeEventHandler (row_changed);
DataRow Row;
for (int i = 0; I <= 3; i++)
... {
row = table1. NewRow ();
row["id"] = i;
row["item"] = i;
Table1. Rows.Add (row);
}
//Accept changes.
Table1. AcceptChanges ();
DataTable table2 = table1. Clone ();
row = table2. NewRow ();
row["id"] = 14;
row["Item" = 774;
Table2. Rows.Add (row);
row = table2. NewRow ();
row["id"] = 12;
row["Item" = 555;
Table2. Rows.Add (row);
row = table2. NewRow ();
row["id"] = 13;
row["Item" = 665;
Table2. Rows.Add (row);
//Merge table2 into the table1.
Table1. Merge (table2);
}

In combination with the above,. NET 2.0 the DataTable from the backstage silently the soldier becomes independently's general.

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.