ADO.NET 从DataTable中获取某列含有的不重复值的几种方式
1个回答
展开全部
/// 相当于select distinct fieldName1,fieldName2,,fieldNamen from sourceTable
/// </summary
/// <param name="tableName"表名</param
/// <param name="sourceTable"源DataTable</param
/// <param name="fieldNames"列名数组</param
/// <returns一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns
public DataTable SelectDistinct(string tableName, DataTable sourceTable, string[] fieldNames){DataTable dt = new DataTable( tableName );
object[] values = new object[fieldNames.Length];
string fields = "";
for ( int i = 0; i < fieldNames.Length; i++ ){dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[ fieldNames[ i ] ].DataType );
fields += fieldNames[ i ] + ",";}fields = fields.Remove( fields.Length - 1, 1 );DataRow lastRow = null;
/// </summary
/// <param name="tableName"表名</param
/// <param name="sourceTable"源DataTable</param
/// <param name="fieldNames"列名数组</param
/// <returns一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns
public DataTable SelectDistinct(string tableName, DataTable sourceTable, string[] fieldNames){DataTable dt = new DataTable( tableName );
object[] values = new object[fieldNames.Length];
string fields = "";
for ( int i = 0; i < fieldNames.Length; i++ ){dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[ fieldNames[ i ] ].DataType );
fields += fieldNames[ i ] + ",";}fields = fields.Remove( fields.Length - 1, 1 );DataRow lastRow = null;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询