Author: Grasshopper Jun Source: Blog Hall
Let’s first look at a piece of WEB Service code.
[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 and 1.0, the return or input parameters of WEB Service cannot be DataTable. This is a well-known knowledge point. The reason is that DataTable does not support serialization like DataSet. In .net 1.1, our way to solve this problem is to use DataSet. But when using DataSet, there is often a feeling of overkill.
Attachment: Using DataTable as the return value of WEB Service in .net 1.1 will report the following exception:
The member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite is an interface, so it cannot be serialized.
In .net 2.0, the same code above works without any problems. The reason is that DataTable in 2.0 implements serialization and deserialization.
In the documentation of VS2005 Beta2, we can see that DataTable in 2.0 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, DataTable only implemented one interface:
Explicit Interface Implementations
System.ComponentModel.IListSource.ContainsListCollection
moves some functions in DataSet to DataTable. There is also a Merge method in 2.0, which merges several data sets.
For the code merging of DataTable, please refer to the code below.
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);
}
To sum up the above, DataTable in .net 2.0 has changed from an unknown soldier in the background to a general in charge.