我们先看一段WEB Service的代码
[WebMethod]
public DataTable GetInfo()
{
OleDbConnection nwindConn = new OleDbConnection(
Provider=MicrosoftJetOLEDB; +
Data Source=D:\\Northwind\\northwindmdb;);
OleDbCommand selectCMD =
new OleDbCommand(SELECT CustomerID CompanyName FROM Customers
nwindConn);
selectCMDCommandTimeout = ;
OleDbDataAdapter custDA = new OleDbDataAdapter();
custDASelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDAFill(custDS Customers);
return custDSTables[];
}
中这是典型的一个错误 中WEB Service 的返回或者输入参数不能是 DataTable这是一个众人皆知的知识点原因就是 DataTable 不象DataSet那样支持序列化 中我们解决这个问题的方法就是使用DataSet但是使用DataSet 的时候经常会有一种杀鸡用牛刀的感觉
附 中使用DataTable作为WEB Service 返回值会报以下异常
类型 SystemComponentModelISite 的成员 SystemComponentModelMarshalByValueComponentSite 是接口因此无法将其序列化
中以上同样的代码则没有任何问题了原因是中 DataTable实现了序列化反序列
在VS Beta 的文档中我们可以看到 中 DataTable实现了以下接口
Explicit Interface Implementations
SystemComponentModelIListSourceget_ContainsListCollection
SystemComponentModelIListSourceGetList
SystemXmlSerializationIXmlSerializableGetSchema
SystemXmlSerializationIXmlSerializableReadXml
SystemXmlSerializationIXmlSerializableWriteXml
而在中DataTable 只实现了一个接口
Explicit Interface Implementations
SystemComponentModelIListSourceContainsListCollection
把DataSet中的一些功能移到 DataTable中 中还有 Merge 方法即合并数个数据集
DataTable的代码合并参看下面代码
private static void DemonstrateMergeTable()
{
DataTable table = new DataTable(Items);
DataColumn column = new DataColumn(id typeof(SystemInt));
DataColumn column = new DataColumn(item typeof(SystemInt));
tableColumnsAdd(column);
tableColumnsAdd(column);
tablePrimaryKey = new DataColumn[] { column };
tableRowChanged += new SystemDataDataRowChangeEventHandler(Row_Changed);
DataRow row;
for (int i = ; i <= ; i++)
{
row = tableNewRow();
row[id] = i;
row[item] = i;
tableRowsAdd(row);
}
// Accept changes
tableAcceptChanges();
DataTable table = tableClone();
row = tableNewRow();
row[id] = ;
row[item] = ;
tableRowsAdd(row);
row = tableNewRow();
row[id] = ;
row[item] = ;
tableRowsAdd(row);
row = tableNewRow();
row[id] = ;
row[item] = ;
tableRowsAdd(row);
// Merge table into the table
tableMerge(table);
}
综合上述 中 DataTable 从后台的默默无问的小兵变成独当一面的大将了