ASP.NET中Dataset的table数据合并、数据截取、数据排序

1、两个相同字段表的合并:

 public static DataSet CombineTables(DataSet _ds, DataTable _dt1, DataTable _dt2)
{
DataSet ds = _ds.Clone();
//ds.Tables[0].Clone();
ds.Tables[].Rows.Clear();
int i = ;
for ( i = ; i < _dt1.Rows.Count; i++)
{
ds.Tables[].ImportRow(_dt1.Rows[i]);
}
for (int j = ; j < _dt2.Rows.Count; j++)
{
ds.Tables[].ImportRow(_dt2.Rows[j]);
}
return ds;
}

2、对表的数据截取:

 public static DataSet CopyScopeRows(DataSet source, int startRowNum, int endRowNum) {
DataSet des = source.Clone();
des.Tables[].Rows.Clear();
for (int i = startRowNum; i < source.Tables[].Rows.Count && i <= endRowNum; i++) {
des.Tables[].ImportRow(source.Tables[].Rows[i]);
}
return des;
}

3、数据行的筛选:

 public static DataSet CopyScopeRows(DataSet source, string filter) {
DataSet des = source.Clone();
DataSet _ds = null;
des.Tables[].Rows.Clear();
try
{
DataRow[] rows = source.Tables[].Select(filter);
foreach (DataRow row in rows) {
des.Tables[].ImportRow(row);
}}
catch (Exception ex) { return _ds; }
finally
{}
return des;
}

4、数据行的筛选,并可以排序:

 public static DataSet CopyScopeRows(DataSet source, string filter, string order) {
DataSet des = source.Clone();
des.Tables[].Rows.Clear();
DataRow[] rows = source.Tables[].Select(filter, order);
foreach (DataRow row in rows)
{
des.Tables[].ImportRow(row);
}
return des;
}

5、数据行的筛选,并可截取数据行:

 public static DataSet CopyScopeRows(DataSet source, string filter, int startRowNum, int endRowNum) {
DataSet des = source.Clone();
des.Tables[].Rows.Clear();
DataRow[] rows = source.Tables[].Select(filter);
for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++)
{
des.Tables[].ImportRow(rows[i]);
}
return des;
}

6、数据行的筛选、排序,并可截取数据行:

 public static DataSet CopyScopeRows(DataSet source, string filter, string order, int startRowNum, int endRowNum) {
DataSet des = source.Clone();
des.Tables[].Rows.Clear();
DataRow[] rows = source.Tables[].Select(filter, order);
for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++) {
des.Tables[].ImportRow(rows[i]);
}
return des;
}
 public static object GetValueByKey(DataTable table, string keyName, object keyValue, string returnName) {
object obj = null;
table.PrimaryKey = new DataColumn[] { table.Columns[keyName] };
DataRow row = table.Rows.Find(keyValue);
if (row != null) { obj = row[returnName]; }
return obj;
}
 public static object GetValueByKeys(DataTable table,  object[] keyValue, string returnName)
{
object obj = null; DataColumn[] keys = new DataColumn[];
keys[] = table.Columns[];
keys[] = table.Columns[];
table.PrimaryKey = keys; DataRow row = table.Rows.Find(keyValue);
if (row != null) { obj = row[returnName]; }
return obj;
}
上一篇:一张图理解RACSignal的Subscription过程


下一篇::单件模式:Singleton