如何把一个DataTable中的某列通过表关系加到另外一个DataTable中
在DataSet中是无法使用Select语句从多个表中自由选择字段组成新的视图的,只能利用DataTable间的关系把一个DataTable中的某列增加到另外一个DataTable中。
原理:
1.在DataTable间建立关系
2.把一个DataTable中的某列增加到另外一个DataTable中
3.如果DataTable不再同一个DataSet中需要合并DataSet
实现代码如下:using System;
using System.Data;
namespace BaseClassLibrary
{
/// <summary>
/// 基础类
/// </summary>
public class DataSetOperate
{
/// <summary>
/// 对DataSet进行处理,建立DataTable表间的关系,向DataTable中增加列
/// </summary>
public DataSetOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 建立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]);
}
/// <summary>
/// 先建立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];
}
}
/// <summary>
/// 在向外建表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];
}
}
}
}
using System.Data;
namespace BaseClassLibrary
{
/// <summary>
/// 基础类
/// </summary>
public class DataSetOperate
{
/// <summary>
/// 对DataSet进行处理,建立DataTable表间的关系,向DataTable中增加列
/// </summary>
public DataSetOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 建立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]);
}
/// <summary>
/// 先建立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];
}
}
/// <summary>
/// 在向外建表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];
}
}
}
}