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

1 public static DataSet CombineTables(DataSet _ds, DataTable _dt1, DataTable _dt2) 2 { 3 DataSet ds = _ds.Clone(); 4 //ds.Tables[0].Clone(); 5 ds.Tables[0].Rows.Clear(); 6 int i = 0; 7 for ( i = 0; i < _dt1.Rows.Count; i++) 8 { 9 ds.Tables[0].ImportRow(_dt1.Rows[i]); 10 } 11 for (int j = 0; j < _dt2.Rows.Count; j++) 12 { 13 ds.Tables[0].ImportRow(_dt2.Rows[j]); 14 } 15 return ds; 16 }
2、对表的数据截取:

1 public static DataSet CopyScopeRows(DataSet source, int startRowNum, int endRowNum) { 2 DataSet des = source.Clone(); 3 des.Tables[0].Rows.Clear(); 4 for (int i = startRowNum; i < source.Tables[0].Rows.Count && i <= endRowNum; i++) { 5 des.Tables[0].ImportRow(source.Tables[0].Rows[i]); 6 } 7 return des; 8 }
3、数据行的筛选:

1 public static DataSet CopyScopeRows(DataSet source, string filter) { 2 DataSet des = source.Clone(); 3 DataSet _ds = null; 4 des.Tables[0].Rows.Clear(); 5 try 6 { 7 DataRow[] rows = source.Tables[0].Select(filter); 8 foreach (DataRow row in rows) { 9 des.Tables[0].ImportRow(row); 10 }} 11 catch (Exception ex) { return _ds; } 12 finally 13 {} 14 return des; 15 }
4、数据行的筛选,并可以排序:

1 public static DataSet CopyScopeRows(DataSet source, string filter, string order) { 2 DataSet des = source.Clone(); 3 des.Tables[0].Rows.Clear(); 4 DataRow[] rows = source.Tables[0].Select(filter, order); 5 foreach (DataRow row in rows) 6 { 7 des.Tables[0].ImportRow(row); 8 } 9 return des; 10 }
5、数据行的筛选,并可截取数据行:

1 public static DataSet CopyScopeRows(DataSet source, string filter, int startRowNum, int endRowNum) { 2 DataSet des = source.Clone(); 3 des.Tables[0].Rows.Clear(); 4 DataRow[] rows = source.Tables[0].Select(filter); 5 for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++) 6 { 7 des.Tables[0].ImportRow(rows[i]); 8 } 9 return des; 10 }
6、数据行的筛选、排序,并可截取数据行:

1 public static DataSet CopyScopeRows(DataSet source, string filter, string order, int startRowNum, int endRowNum) { 2 DataSet des = source.Clone(); 3 des.Tables[0].Rows.Clear(); 4 DataRow[] rows = source.Tables[0].Select(filter, order); 5 for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++) { 6 des.Tables[0].ImportRow(rows[i]); 7 } 8 return des; 9 }

1 public static object GetValueByKey(DataTable table, string keyName, object keyValue, string returnName) { 2 object obj = null; 3 table.PrimaryKey = new DataColumn[] { table.Columns[keyName] }; 4 DataRow row = table.Rows.Find(keyValue); 5 if (row != null) { obj = row[returnName]; } 6 return obj; 7 }

1 public static object GetValueByKeys(DataTable table, object[] keyValue, string returnName) 2 { 3 object obj = null; 4 5 DataColumn[] keys = new DataColumn[2]; 6 keys[1] = table.Columns[0]; 7 keys[0] = table.Columns[2]; 8 table.PrimaryKey = keys; 9 10 DataRow row = table.Rows.Find(keyValue); 11 if (row != null) { obj = row[returnName]; } 12 return obj; 13 }