我有两个数据访问层的方法,分别返回两个 DataTable ,一个DataTable 是查询的人的基本信息, 比如: playerid 张三 男 22岁
另一个DataTable 是查询人报的项目名称 比如: playerid 100米 200米要得到的效果: 张三 男 22岁 100米 200米我要在业务层把两个DataTable 合成一个,因为在界面层只能接受一个DataTable 或 DataSet
另一个DataTable 是查询人报的项目名称 比如: playerid 100米 200米要得到的效果: 张三 男 22岁 100米 200米我要在业务层把两个DataTable 合成一个,因为在界面层只能接受一个DataTable 或 DataSet
第二个 DataTable 是程序中动态生成的临时表,然后查询出临时表的数据,是通过SQL编程来实现的。其实我把这个临时表建在数据库中,通过SQL编程来给这个表添数据,再把这个表跟前面的表一起做成视图也是可行的,但是这样就在数据库中留下了一个没有用的表了,也比较麻烦。如果有办法把两个 DataTable 合在一起就什么问题都解决了,请大家多提思路啊!
{
DataTable mergedTable = CreateMergedTable(); foreach (DataRow arow in table1.Rows)
{
string playerId = arow["PlayerId"].ToString();
DataRow brow = table2.Rows.Find(playerId); if (brow != null)
{
DataRow newRow = mergedTable.NewRow();
newRow["PlayerId"] = playerId;
newRow["Name"] = arow["Name"].ToString();
newRow["Sex"] = arow["Sex"].ToString();
newRow["Age"] = arow["Age"].ToString();
newRow["Length1"] = brow["Length1"].ToString();
newRow["Length1"] = brow["Length2"].ToString();
}
} return mergedTable;
}private DataTable CreateMergedTable()
{
DataTable table = new DataTable();
DataColumnCollection columns = table.Columns;
columns.Add("PlayerId", typeof(System.Int32));
columns.Add("Name", typeof(System.String));
columns.Add("Sex", typeof(System.String));
columns.Add("Age", typeof(System.String));
columns.Add("Length1", typeof(System.String));
columns.Add("Length2", typeof(System.String));
table.PrimaryKey = new DataColumn[] {columns["PlayerId"]};
return table;
}
http://www.microsoft.com/china/MSDN/library/data/dataAccess/ADONETEXP.mspx