在dataset中是无法使用select语句从多个表中自由选择字段组成新的视图的,只能利用datatable间的关系把一个datatable中的某列增加到另外一个datatable中。
原理:
1.在datatable间建立关系
2.把一个datatable中的某列增加到另外一个datatable中
3.如果datatable不再同一个dataset中需要合并dataset
实现代码如下:
using System;
using System.Data;
namespace BaseClassLibrary
{
public class DataSetOperate
{
/// 对DataSet进行处理,建立DataTable表间的关系,向DataTable中增加列
public DataSetOperate()
{
}
/// 建立DataTable表间的关系
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="primaryColumnName">主键列名称</param>
/// <param name="foreignColumnName">外键列名称</param>
public void newRelation(DataSet ds,string primaryTableName,string foreignTableName,string primaryColumnName,string foreignColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
ds.Relations.Add(primaryTableName foreignTableName,primaryTable.Columns[primaryColumnName],foreignTable.Columns[foreignColumnName]);
}
/// 先建立DataSet间关系,在向外建表DataTabe中增加列
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="primaryColumnName">主键列名称</param>
/// <param name="foreignColumnName">外键列名称</param>
/// <param name="addColumnName">主键表中列名称</param>
/// <param name="newColumnName">外键表中新增列名称</param>
public void addColumnWithRelation(DataSet ds,string primaryTableName,string foreignTableName,string primaryColumnName,string foreignColumnName,string addColumnName,string newColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
ds.Relations.Add(primaryTableName foreignTableName,primaryTable.Columns[primaryColumnName],foreignTable.Columns[foreignColumnName]);
DataColumn newColumn = new DataColumn(newColumnName,ds.Tables[primaryTableName].Columns[addColumnName].DataType);
ds.Tables[foreignTableName].Columns.Add(newColumn);
int rowCount = ds.Tables[foreignTableName].Rows.Count;
for(int i = 0;i < rowCount;i++ )
{
DataRow parentCustomerDR = foreignTable.Rows[i].GetParentRow(primaryTableName foreignTableName);
foreignTable.Rows[i][newColumnName] = parentCustomerDR[addColumnName];
}
}
/// 在向外建表DataTabe中增加列
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="addColumnName">主键表中列名称</param>
/// <param name="newColumnName">外键表中新增列名称</param>
public void addColumn(DataSet ds,string primaryTableName,string foreignTableName,string addColumnName,string newColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
DataColumn newColumn = new DataColumn(newColumnName,ds.Tables[primaryTableName].Columns[addColumnName].DataType);
ds.Tables[foreignTableName].Columns.Add(newColumn);
int rowCount = ds.Tables[foreignTableName].Rows.Count;
for(int i = 0;i < rowCount;i++ )
{
DataRow parentCustomerDR = foreignTable.Rows[i].GetParentRow(primaryTableName foreignTableName);
foreignTable.Rows[i][newColumnName] = parentCustomerDR[addColumnName];
}
}
}
}
原理:
1.在datatable间建立关系
2.把一个datatable中的某列增加到另外一个datatable中
3.如果datatable不再同一个dataset中需要合并dataset
实现代码如下:
using System;
using System.Data;
namespace BaseClassLibrary
{
public class DataSetOperate
{
/// 对DataSet进行处理,建立DataTable表间的关系,向DataTable中增加列
public DataSetOperate()
{
}
/// 建立DataTable表间的关系
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="primaryColumnName">主键列名称</param>
/// <param name="foreignColumnName">外键列名称</param>
public void newRelation(DataSet ds,string primaryTableName,string foreignTableName,string primaryColumnName,string foreignColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
ds.Relations.Add(primaryTableName foreignTableName,primaryTable.Columns[primaryColumnName],foreignTable.Columns[foreignColumnName]);
}
/// 先建立DataSet间关系,在向外建表DataTabe中增加列
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="primaryColumnName">主键列名称</param>
/// <param name="foreignColumnName">外键列名称</param>
/// <param name="addColumnName">主键表中列名称</param>
/// <param name="newColumnName">外键表中新增列名称</param>
public void addColumnWithRelation(DataSet ds,string primaryTableName,string foreignTableName,string primaryColumnName,string foreignColumnName,string addColumnName,string newColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
ds.Relations.Add(primaryTableName foreignTableName,primaryTable.Columns[primaryColumnName],foreignTable.Columns[foreignColumnName]);
DataColumn newColumn = new DataColumn(newColumnName,ds.Tables[primaryTableName].Columns[addColumnName].DataType);
ds.Tables[foreignTableName].Columns.Add(newColumn);
int rowCount = ds.Tables[foreignTableName].Rows.Count;
for(int i = 0;i < rowCount;i++ )
{
DataRow parentCustomerDR = foreignTable.Rows[i].GetParentRow(primaryTableName foreignTableName);
foreignTable.Rows[i][newColumnName] = parentCustomerDR[addColumnName];
}
}
/// 在向外建表DataTabe中增加列
/// </summary>
/// <param name="ds">需要建立关系的DataSet</param>
/// <param name="primaryTableName">主键表名称</param>
/// <param name="foreignTableName">外键表名称</param>
/// <param name="addColumnName">主键表中列名称</param>
/// <param name="newColumnName">外键表中新增列名称</param>
public void addColumn(DataSet ds,string primaryTableName,string foreignTableName,string addColumnName,string newColumnName)
{
DataTable primaryTable = ds.Tables[primaryTableName];
DataTable foreignTable = ds.Tables[foreignTableName];
DataColumn newColumn = new DataColumn(newColumnName,ds.Tables[primaryTableName].Columns[addColumnName].DataType);
ds.Tables[foreignTableName].Columns.Add(newColumn);
int rowCount = ds.Tables[foreignTableName].Rows.Count;
for(int i = 0;i < rowCount;i++ )
{
DataRow parentCustomerDR = foreignTable.Rows[i].GetParentRow(primaryTableName foreignTableName);
foreignTable.Rows[i][newColumnName] = parentCustomerDR[addColumnName];
}
}
}
}