- 浏览: 17725 次
- 性别:
- 来自: 成都
文章分类
最新评论
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.ComponentModel;
namespace Soft.Lib
{
/// <summary>
/// DataTable操作
/// </summary>
public abstract class DataTableHelper
{
/// <summary>
/// 将DataTable按起始位置和移动及移动方向进行移动并返回新的DataTable
/// </summary>
/// <param name="dt">要移动的DataTable</param>
/// <param name="StartRow">要移动的行(索引从1开始)</param>
/// <param name="MoveCount">要移动的行数</param>
/// <param name="MoveUp">是否上移(true为上移false为下移)</param>
/// <returns>将移动完成后的DataTable返回,如果移动有误的话将返回原Table</returns>
static public DataTable GetNewTable(DataTable dt, int StartRow, int MoveCount, bool MoveUp)
{
#region 将DataTable按起始位置和移动及移动方向进行移动并返回新的DataTable
DataRow dr = dt.NewRow();
dr.ItemArray = dt.Rows[StartRow - 1].ItemArray;
int RowCount = dt.Rows.Count;
if (StartRow > RowCount)//移动的行在行数外面
return dt;
if (MoveUp)//上移
{
if (StartRow - MoveCount <= 0) { }
else
{
for (int i = 0; i < MoveCount; i++)
dt.Rows[StartRow - i - 1].ItemArray = dt.Rows[StartRow - i - 2].ItemArray;
dt.Rows[StartRow - MoveCount - 1].ItemArray = dr.ItemArray;
}
}
else//下移
{
if (StartRow + MoveCount > RowCount) { }
else
{
for (int i = 0; i < MoveCount; i++)
dt.Rows[StartRow + i - 1].ItemArray = dt.Rows[StartRow + i].ItemArray;
dt.Rows[StartRow + MoveCount - 1].ItemArray = dr.ItemArray;
}
}
return dt;
#endregion
}
/// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTable(DataTable dt, string filter)
{
#region 根据条件过滤表
DataTable newTable = dt.Clone();
DataRow[] drs = dt.Select(filter);
foreach (DataRow dr in drs)
{
object[] arr = dr.ItemArray;
DataRow newrow = newTable.NewRow();
for (int i = 0; i < arr.Length; i++)
newrow[i] = arr[i];
newTable.Rows.Add(newrow);
}
return newTable;
#endregion
}
// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTable(DataTable dt, string filter, string sort)
{
#region 根据条件过滤表
DataTable newTable = dt.Clone();
DataRow[] drs = dt.Select(filter, sort);
foreach (DataRow dr in drs)
{
object[] arr = dr.ItemArray;
DataRow newrow = newTable.NewRow();
for (int i = 0; i < arr.Length; i++)
newrow[i] = arr[i];
newTable.Rows.Add(newrow);
}
return newTable;
#endregion
}
/// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTableByDataView(DataTable dt, string filter)
{
#region 根据条件过滤表
DataView dv = new DataView(dt);
dv.RowFilter = filter;
return dv.Table;
#endregion
}
/// <summary>
/// 返回两个表的关联数据,关联后的表中只包含第一个表的字段和第二个表需要的字段
/// </summary>
/// <param name="FirstTable">第一个表(左表)</param>
/// <param name="SecondTable">第二个表(右表)</param>
/// <param name="FJC">第一个表要与第二个表关联的字段</param>
/// <param name="SJC">第二个表要与第一个表关联的字段</param>
/// <param name="SJCNeed">第二个表中需要保留的字段</param>
/// <param name="IsLeftOuter">是否是左外连接,否则为内连接</param>
/// <returns></returns>
private static DataTable LeftOuterOrJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed, bool IsLeftOuter)
{
//创建一个新的DataTable
DataTable table = new DataTable("LJoin");
// Use a DataSet to leverage DataRelation
using (DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
//ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
DataTable First = FirstTable.Copy();
DataTable Second = SecondTable.Copy();
First.TableName = "FirstTable";
Second.TableName = "SecondTable";
for (int i = 0; i < Second.Columns.Count; i++)//删除第二个表中不需要的字段
{
bool Conten = false;
for (int j = 0; j < SJC.Length; j++)
{
if (SJC[j].ColumnName.ToUpper().Trim() == Second.Columns[i].ColumnName.ToUpper().Trim())
{
Conten = true;
break;
}
}
if (!Conten)
{
for (int j = 0; j < SJCNeed.Length; j++)
{
if (SJCNeed[j].ColumnName.ToUpper().Trim() == Second.Columns[i].ColumnName.ToUpper().Trim())
{
Conten = true;
break;
}
}
}
if (!Conten)
{
Second.Columns.RemoveAt(i);
i--;
}
}
ds.Tables.AddRange(new DataTable[] { First, Second });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = 0; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = 0; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
}
//创建关联
DataRelation r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);
ds.Relations.Add(r);
//为关联表创建列
for (int i = 0; i < First.Columns.Count; i++)
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for (int i = 0; i < Second.Columns.Count; i++)
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if (!table.Columns.Contains(Second.Columns[i].ColumnName))
{
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
}
else
{
table.Columns.Add(Second.Columns[i].ColumnName + "_Second", Second.Columns[i].DataType);
}
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
if (childrows != null && childrows.Length > 0)
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
else
{
if (IsLeftOuter)
{
object[] parentarray = firstrow.ItemArray;
DataRow secondrow = Second.NewRow();
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
}
table.EndLoadData();
}
for (int i = 0; i < table.Columns.Count; i++)
{
if (table.Columns[i].ColumnName.EndsWith("_Second"))
{
table.Columns.RemoveAt(i);
i--;
}
}
return table;
}
/// <summary>
/// 返回两个表的关联数据
/// </summary>
/// <param name="FirstTable">第一个表(左表)</param>
/// <param name="SecondTable">第二个表(右表)</param>
/// <param name="FJC">第一个表要与第二个表关联的字段</param>
/// <param name="SJC">第二个表要与第一个表关联的字段</param>
/// <param name="IsLeftOuter">是否是左外连接,否则为内连接</param>
/// <returns></returns>
private static DataTable LeftOuterOrJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, bool IsLeftOuter)
{
//创建一个新的DataTable
DataTable table = new DataTable("Join");
// Use a DataSet to leverage DataRelation
using (DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
//ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
DataTable First = FirstTable.Copy();
DataTable Second = SecondTable.Copy();
First.TableName = "FirstTable";
Second.TableName = "SecondTable";
ds.Tables.AddRange(new DataTable[] { First, Second });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = 0; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = 0; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
}
//创建关联
DataRelation r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);
ds.Relations.Add(r);
//为关联表创建列
for (int i = 0; i < First.Columns.Count; i++)
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for (int i = 0; i < Second.Columns.Count; i++)
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if (!table.Columns.Contains(Second.Columns[i].ColumnName))
{
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
}
else
{
table.Columns.Add(Second.Columns[i].ColumnName + "_Second", Second.Columns[i].DataType);
}
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
if (childrows != null && childrows.Length > 0)
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
else if (IsLeftOuter)
{
object[] parentarray = firstrow.ItemArray;
DataRow secondrow = Second.NewRow();
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
table.EndLoadData();
}
return table;
}
/// <summary>
///
/// </summary>
/// <param name="FirstTable"></param>
/// <param name="SecondTable"></param>
/// <param name="FJC"></param>
/// <param name="SJC"></param>
/// <returns></returns>
public static DataTable Join(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, false);
}
public static DataTable Join(DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
{
return Join(First, Second, new DataColumn[] { FJC }, new DataColumn[] { SJC });
}
public static DataTable Join(DataTable First, DataTable Second, string FJC, string SJC)
{
return Join(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] });
}
public static DataTable Join(DataTable First, DataTable Second, string []FJC, string []SJC)
{
if (First == null || Second == null || FJC == null || SJC == null )
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
return Join(First, Second, FJColumns, SJColumns);
}
public static DataTable Join(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, SJCNeed, false );
}
public static DataTable Join(DataTable First, DataTable Second, string FJC, string SJC, params string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return Join(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] }, SecondNeedScolumns);
}
public static DataTable Join(DataTable First, DataTable Second, string[] FJC, string[] SJC ,string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null||SecondNeedS==null )
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return Join(First, Second, FJColumns, SJColumns, SecondNeedScolumns);
}
public static DataTable LeftOuterJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, true );
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string FJC, string SJC)
{
return LeftOuterJoin(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] });
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string[] FJC, string[] SJC)
{
if (First == null || Second == null || FJC == null || SJC == null)
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
return LeftOuterJoin(First, Second, FJColumns, SJColumns);
}
public static DataTable LeftOuterJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, SJCNeed, true);
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string FJC, string SJC, params string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return LeftOuterJoin(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] }, SecondNeedScolumns);
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string[] FJC, string[] SJC, string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return LeftOuterJoin(First, Second, FJColumns, SJColumns, SecondNeedScolumns);
}
/// <summary>
/// 将Datatable封装成泛型
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public static IEnumerable<DataRow> TableToIEnumerable(DataTable table)
{
return new LinqList<DataRow>(table.Rows);
}
private static bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
{
// bool result = true;
for (int i = 0; i < columns.Count; i++)
{
//result &= ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]);
if (!ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]))
{
return false;
}
}
return true ;
}
private static bool ColumnEqual(object objectA, object objectB)
{
if (objectA == DBNull.Value && objectB == DBNull.Value)
{
return true;
}
if (objectA == DBNull.Value || objectB == DBNull.Value)
{
return false;
}
return (objectA.Equals(objectB));
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于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 static 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;
foreach (DataRow dr in sourceTable.Select("", fields))
{
if (lastRow == null || !(RowEqual(lastRow, dr, dt.Columns)))
{
lastRow = dr;
for (int i = 0; i < fieldNames.Length; i++)
{
values[i] = dr[fieldNames[i]];
}
dt.Rows.Add(values);
}
}
return dt;
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于select distinct fieldName1,fieldName2,fieldNamen from sourceTable
/// </summary>
/// <param name="sourceTable">源DataTable</param>
/// <param name="fieldNames">列名数组</param>
/// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>
public static DataTable SelectDistinct( DataTable sourceTable, string[] fieldNames)
{
DataTable dt = new DataTable();
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;
foreach (DataRow dr in sourceTable.Select("", fields))
{
if (lastRow == null || !(RowEqual(lastRow, dr, dt.Columns)))
{
lastRow = dr;
for (int i = 0; i < fieldNames.Length; i++)
{
values[i] = dr[fieldNames[i]];
}
dt.Rows.Add(values);
}
}
return dt;
}
/// <summary>
/// 根据表的三个个字段 去掉重复行 2011-12-12
/// </summary>
/// <param name="dt"></param>
/// <param name="filed1"></param>
/// <param name="filed2"></param>
/// <param name="filed3"></param>
static public DataTable GetTable(DataTable dt, string filed1, string filed2, string filed3)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
bool Have = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Have = false;
for (int j = i + 1; j < dt.Rows.Count; j++)
{
if (dt.Rows[i][filed1].ToString().Trim() == dt.Rows[j][filed1].ToString().Trim() && dt.Rows[i][filed2].ToString().Trim() == dt.Rows[j][filed2].ToString().Trim() && dt.Rows[i][filed3].ToString().Trim() == dt.Rows[j][filed3].ToString().Trim())
{
Have = true;
break;
}
}
if (!Have)
{
newdt.ImportRow(dt.Rows[i]);
}
}
}
return newdt;
}
}
public class LinqList<T> : IEnumerable<T>, IEnumerable
{
IEnumerable items;
internal LinqList(IEnumerable items)
{
this.items = items;
}
#region IEnumerable<DataRow> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (T item in items) yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
{
IEnumerable<T> ie = this;
return ie.GetEnumerator();
}
#endregion
}
public static class Projecttable
{
public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
ret.Columns.Add(dp.Name, dp.PropertyType);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.ComponentModel;
namespace Soft.Lib
{
/// <summary>
/// DataTable操作
/// </summary>
public abstract class DataTableHelper
{
/// <summary>
/// 将DataTable按起始位置和移动及移动方向进行移动并返回新的DataTable
/// </summary>
/// <param name="dt">要移动的DataTable</param>
/// <param name="StartRow">要移动的行(索引从1开始)</param>
/// <param name="MoveCount">要移动的行数</param>
/// <param name="MoveUp">是否上移(true为上移false为下移)</param>
/// <returns>将移动完成后的DataTable返回,如果移动有误的话将返回原Table</returns>
static public DataTable GetNewTable(DataTable dt, int StartRow, int MoveCount, bool MoveUp)
{
#region 将DataTable按起始位置和移动及移动方向进行移动并返回新的DataTable
DataRow dr = dt.NewRow();
dr.ItemArray = dt.Rows[StartRow - 1].ItemArray;
int RowCount = dt.Rows.Count;
if (StartRow > RowCount)//移动的行在行数外面
return dt;
if (MoveUp)//上移
{
if (StartRow - MoveCount <= 0) { }
else
{
for (int i = 0; i < MoveCount; i++)
dt.Rows[StartRow - i - 1].ItemArray = dt.Rows[StartRow - i - 2].ItemArray;
dt.Rows[StartRow - MoveCount - 1].ItemArray = dr.ItemArray;
}
}
else//下移
{
if (StartRow + MoveCount > RowCount) { }
else
{
for (int i = 0; i < MoveCount; i++)
dt.Rows[StartRow + i - 1].ItemArray = dt.Rows[StartRow + i].ItemArray;
dt.Rows[StartRow + MoveCount - 1].ItemArray = dr.ItemArray;
}
}
return dt;
#endregion
}
/// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTable(DataTable dt, string filter)
{
#region 根据条件过滤表
DataTable newTable = dt.Clone();
DataRow[] drs = dt.Select(filter);
foreach (DataRow dr in drs)
{
object[] arr = dr.ItemArray;
DataRow newrow = newTable.NewRow();
for (int i = 0; i < arr.Length; i++)
newrow[i] = arr[i];
newTable.Rows.Add(newrow);
}
return newTable;
#endregion
}
// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTable(DataTable dt, string filter, string sort)
{
#region 根据条件过滤表
DataTable newTable = dt.Clone();
DataRow[] drs = dt.Select(filter, sort);
foreach (DataRow dr in drs)
{
object[] arr = dr.ItemArray;
DataRow newrow = newTable.NewRow();
for (int i = 0; i < arr.Length; i++)
newrow[i] = arr[i];
newTable.Rows.Add(newrow);
}
return newTable;
#endregion
}
/// <summary>
/// 根据条件过滤表
/// </summary>
/// <param name="dt">未过滤之前的表</param>
/// <param name="filter">过滤条件</param>
/// <returns>返回过滤后的表</returns>
static public DataTable GetNewTableByDataView(DataTable dt, string filter)
{
#region 根据条件过滤表
DataView dv = new DataView(dt);
dv.RowFilter = filter;
return dv.Table;
#endregion
}
/// <summary>
/// 返回两个表的关联数据,关联后的表中只包含第一个表的字段和第二个表需要的字段
/// </summary>
/// <param name="FirstTable">第一个表(左表)</param>
/// <param name="SecondTable">第二个表(右表)</param>
/// <param name="FJC">第一个表要与第二个表关联的字段</param>
/// <param name="SJC">第二个表要与第一个表关联的字段</param>
/// <param name="SJCNeed">第二个表中需要保留的字段</param>
/// <param name="IsLeftOuter">是否是左外连接,否则为内连接</param>
/// <returns></returns>
private static DataTable LeftOuterOrJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed, bool IsLeftOuter)
{
//创建一个新的DataTable
DataTable table = new DataTable("LJoin");
// Use a DataSet to leverage DataRelation
using (DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
//ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
DataTable First = FirstTable.Copy();
DataTable Second = SecondTable.Copy();
First.TableName = "FirstTable";
Second.TableName = "SecondTable";
for (int i = 0; i < Second.Columns.Count; i++)//删除第二个表中不需要的字段
{
bool Conten = false;
for (int j = 0; j < SJC.Length; j++)
{
if (SJC[j].ColumnName.ToUpper().Trim() == Second.Columns[i].ColumnName.ToUpper().Trim())
{
Conten = true;
break;
}
}
if (!Conten)
{
for (int j = 0; j < SJCNeed.Length; j++)
{
if (SJCNeed[j].ColumnName.ToUpper().Trim() == Second.Columns[i].ColumnName.ToUpper().Trim())
{
Conten = true;
break;
}
}
}
if (!Conten)
{
Second.Columns.RemoveAt(i);
i--;
}
}
ds.Tables.AddRange(new DataTable[] { First, Second });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = 0; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = 0; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
}
//创建关联
DataRelation r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);
ds.Relations.Add(r);
//为关联表创建列
for (int i = 0; i < First.Columns.Count; i++)
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for (int i = 0; i < Second.Columns.Count; i++)
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if (!table.Columns.Contains(Second.Columns[i].ColumnName))
{
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
}
else
{
table.Columns.Add(Second.Columns[i].ColumnName + "_Second", Second.Columns[i].DataType);
}
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
if (childrows != null && childrows.Length > 0)
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
else
{
if (IsLeftOuter)
{
object[] parentarray = firstrow.ItemArray;
DataRow secondrow = Second.NewRow();
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
}
table.EndLoadData();
}
for (int i = 0; i < table.Columns.Count; i++)
{
if (table.Columns[i].ColumnName.EndsWith("_Second"))
{
table.Columns.RemoveAt(i);
i--;
}
}
return table;
}
/// <summary>
/// 返回两个表的关联数据
/// </summary>
/// <param name="FirstTable">第一个表(左表)</param>
/// <param name="SecondTable">第二个表(右表)</param>
/// <param name="FJC">第一个表要与第二个表关联的字段</param>
/// <param name="SJC">第二个表要与第一个表关联的字段</param>
/// <param name="IsLeftOuter">是否是左外连接,否则为内连接</param>
/// <returns></returns>
private static DataTable LeftOuterOrJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, bool IsLeftOuter)
{
//创建一个新的DataTable
DataTable table = new DataTable("Join");
// Use a DataSet to leverage DataRelation
using (DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
//ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
DataTable First = FirstTable.Copy();
DataTable Second = SecondTable.Copy();
First.TableName = "FirstTable";
Second.TableName = "SecondTable";
ds.Tables.AddRange(new DataTable[] { First, Second });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = 0; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = 0; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
}
//创建关联
DataRelation r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);
ds.Relations.Add(r);
//为关联表创建列
for (int i = 0; i < First.Columns.Count; i++)
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for (int i = 0; i < Second.Columns.Count; i++)
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if (!table.Columns.Contains(Second.Columns[i].ColumnName))
{
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
}
else
{
table.Columns.Add(Second.Columns[i].ColumnName + "_Second", Second.Columns[i].DataType);
}
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
if (childrows != null && childrows.Length > 0)
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
else if (IsLeftOuter)
{
object[] parentarray = firstrow.ItemArray;
DataRow secondrow = Second.NewRow();
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
table.EndLoadData();
}
return table;
}
/// <summary>
///
/// </summary>
/// <param name="FirstTable"></param>
/// <param name="SecondTable"></param>
/// <param name="FJC"></param>
/// <param name="SJC"></param>
/// <returns></returns>
public static DataTable Join(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, false);
}
public static DataTable Join(DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
{
return Join(First, Second, new DataColumn[] { FJC }, new DataColumn[] { SJC });
}
public static DataTable Join(DataTable First, DataTable Second, string FJC, string SJC)
{
return Join(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] });
}
public static DataTable Join(DataTable First, DataTable Second, string []FJC, string []SJC)
{
if (First == null || Second == null || FJC == null || SJC == null )
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
return Join(First, Second, FJColumns, SJColumns);
}
public static DataTable Join(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, SJCNeed, false );
}
public static DataTable Join(DataTable First, DataTable Second, string FJC, string SJC, params string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return Join(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] }, SecondNeedScolumns);
}
public static DataTable Join(DataTable First, DataTable Second, string[] FJC, string[] SJC ,string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null||SecondNeedS==null )
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return Join(First, Second, FJColumns, SJColumns, SecondNeedScolumns);
}
public static DataTable LeftOuterJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, true );
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string FJC, string SJC)
{
return LeftOuterJoin(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] });
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string[] FJC, string[] SJC)
{
if (First == null || Second == null || FJC == null || SJC == null)
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
return LeftOuterJoin(First, Second, FJColumns, SJColumns);
}
public static DataTable LeftOuterJoin(DataTable FirstTable, DataTable SecondTable, DataColumn[] FJC, DataColumn[] SJC, DataColumn[] SJCNeed)
{
return LeftOuterOrJoin(FirstTable, SecondTable, FJC, SJC, SJCNeed, true);
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string FJC, string SJC, params string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return LeftOuterJoin(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { Second.Columns[SJC] }, SecondNeedScolumns);
}
public static DataTable LeftOuterJoin(DataTable First, DataTable Second, string[] FJC, string[] SJC, string[] SecondNeedS)
{
if (First == null || Second == null || FJC == null || SJC == null || SecondNeedS == null)
{
return null;
}
DataColumn[] FJColumns = new DataColumn[FJC.Length];
for (int i = 0; i < FJC.Length; i++)
{
FJColumns[i] = First.Columns[FJC[i].Trim()];
}
DataColumn[] SJColumns = new DataColumn[SJC.Length];
for (int i = 0; i < SJC.Length; i++)
{
SJColumns[i] = Second.Columns[SJC[i].Trim()];
}
DataColumn[] SecondNeedScolumns = new DataColumn[SecondNeedS.Length];
for (int i = 0; i < SecondNeedS.Length; i++)
{
SecondNeedScolumns[i] = Second.Columns[SecondNeedS[i].Trim()];
}
return LeftOuterJoin(First, Second, FJColumns, SJColumns, SecondNeedScolumns);
}
/// <summary>
/// 将Datatable封装成泛型
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public static IEnumerable<DataRow> TableToIEnumerable(DataTable table)
{
return new LinqList<DataRow>(table.Rows);
}
private static bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
{
// bool result = true;
for (int i = 0; i < columns.Count; i++)
{
//result &= ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]);
if (!ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]))
{
return false;
}
}
return true ;
}
private static bool ColumnEqual(object objectA, object objectB)
{
if (objectA == DBNull.Value && objectB == DBNull.Value)
{
return true;
}
if (objectA == DBNull.Value || objectB == DBNull.Value)
{
return false;
}
return (objectA.Equals(objectB));
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于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 static 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;
foreach (DataRow dr in sourceTable.Select("", fields))
{
if (lastRow == null || !(RowEqual(lastRow, dr, dt.Columns)))
{
lastRow = dr;
for (int i = 0; i < fieldNames.Length; i++)
{
values[i] = dr[fieldNames[i]];
}
dt.Rows.Add(values);
}
}
return dt;
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于select distinct fieldName1,fieldName2,fieldNamen from sourceTable
/// </summary>
/// <param name="sourceTable">源DataTable</param>
/// <param name="fieldNames">列名数组</param>
/// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>
public static DataTable SelectDistinct( DataTable sourceTable, string[] fieldNames)
{
DataTable dt = new DataTable();
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;
foreach (DataRow dr in sourceTable.Select("", fields))
{
if (lastRow == null || !(RowEqual(lastRow, dr, dt.Columns)))
{
lastRow = dr;
for (int i = 0; i < fieldNames.Length; i++)
{
values[i] = dr[fieldNames[i]];
}
dt.Rows.Add(values);
}
}
return dt;
}
/// <summary>
/// 根据表的三个个字段 去掉重复行 2011-12-12
/// </summary>
/// <param name="dt"></param>
/// <param name="filed1"></param>
/// <param name="filed2"></param>
/// <param name="filed3"></param>
static public DataTable GetTable(DataTable dt, string filed1, string filed2, string filed3)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
bool Have = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Have = false;
for (int j = i + 1; j < dt.Rows.Count; j++)
{
if (dt.Rows[i][filed1].ToString().Trim() == dt.Rows[j][filed1].ToString().Trim() && dt.Rows[i][filed2].ToString().Trim() == dt.Rows[j][filed2].ToString().Trim() && dt.Rows[i][filed3].ToString().Trim() == dt.Rows[j][filed3].ToString().Trim())
{
Have = true;
break;
}
}
if (!Have)
{
newdt.ImportRow(dt.Rows[i]);
}
}
}
return newdt;
}
}
public class LinqList<T> : IEnumerable<T>, IEnumerable
{
IEnumerable items;
internal LinqList(IEnumerable items)
{
this.items = items;
}
#region IEnumerable<DataRow> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (T item in items) yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
{
IEnumerable<T> ie = this;
return ie.GetEnumerator();
}
#endregion
}
public static class Projecttable
{
public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
ret.Columns.Add(dp.Name, dp.PropertyType);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}
}
}
发表评论
-
取CPU的序列号
2016-12-28 15:12 0可以取CPU的序列号。 string cpuInfo = ... -
C#中控制输入法
2016-12-28 15:10 0其实,在seaman_X提供的链接中,已经对如何在Wi ... -
C#中得到网卡号
2016-12-28 15:12 0using System.Management; //g ... -
C#怎样打开关闭CDROM
2016-12-28 15:08 364C#怎样打开关闭CDROM? using Syste ... -
C#如何取硬件标志
2016-12-28 15:10 414using System; using System.R ... -
C#里的InputBox
2016-12-28 15:06 788在.NET Framework中并没有类似VB中的InputB ... -
C#串口操作
2016-12-28 15:04 660#region Using using System; ... -
C#编码标准
2016-12-28 15:06 438C#编码标准--编码习惯 1. 避免将多个类放在一 ... -
C#基础教程-c#实例教程,适合初学者
2016-08-22 11:13 1444C#基础教程-c#实例教程,适合初学者。 第一章 C#语言基础 ... -
C#必须掌握的系统类
2016-07-28 10:31 2610系统类 Type类,Object类,String类, Arr ... -
CookieHelper.cs
2016-07-28 10:25 455using System; using System.Coll ... -
SqlHelper.cs
2016-07-28 10:24 421using System.Data; using Syste ...
相关推荐
附件包括 c# 对 DataTable各种常用操作的源代码 包括DataTable按起始位置和移动及移动方向进行移动并返回新的DataTable ;根据条件过滤表 ;返回两个表的关联数据,关联后的表中只包含第一个表的字段和第二个表需要...
DataTableHelper 用c# 对DataTable进行操作的类库汇总 源代码 京华志&精华志出品 希望大家互相学习,互相进步 支持CSDN 支持微软 主要包括C# ASP.NET SQLDBA 源码 毕业设计 开题报告 答辩PPT等
在Java开发中,特别是在Android应用开发中,常常需要处理数据集合,C#中的DataTable是一种非常方便的数据结构,它允许我们动态地存储和操作表格数据。然而,Java原生API并没有提供与之对应的类。这篇博客(博文链接...
C# DataTable 常用操作总结 在 C# 中,DataTable 是一个非常重要的数据结构,用于存储和操作数据。本文将总结 DataTable 的一些常用操作,包括创建 DataTable、添加列和行、选择行、复制 DataTable、操作行、评估另...
在C#编程中,DataTable是System.Data命名空间中的一个类,它用于存储和操作数据集中的数据表。在处理大量数据时,有时我们需要删除DataTable中的特定行或多行。本篇将详细介绍如何在C#中有效地实现DataTable的多行...
在C#编程中,将数据从DataTable对象转换到ListView控件是一种常见的数据展示技术,尤其在Windows Forms应用程序中。此过程涉及多个步骤,确保数据能够准确无误地从数据库或数据源传递到用户界面,从而提供清晰、有序...
C#-DataTable操作类(添加自增列、检查是否为数据行、DataTable转换为List、List转换为DataTable,DataTable排序、DataRow转自定义实体)
利用C#saveFileDialog控件,把DataTable中的数据保存输出的文本格式
用C#实现对DataTable的JOIN,GROUP BY,FILTER,UNIONALL,DISTINCT
在C#编程环境中,WinForms是一个常用的开发桌面应用程序的框架。在WinForms应用中,有时候我们需要将数据表(如DataTable)的数据打印出来,这在报表生成、数据分析等场景下非常常见。本文将深入探讨如何使用C# ...
在.NET框架中,C#是一种常用的编程语言,用于构建各种应用程序,包括桌面应用、Web应用以及服务端应用。DataTable是System.Data命名空间中的一个类,它代表了一个可以在内存中存储和操作数据的表格。在处理数据库...
在.NET框架中,C#是一种常用的编程语言,用于开发各种应用程序,包括与数据处理相关的任务。本示例涉及的核心知识点是将DataTable中的数据导出到Excel文件,这在数据分析、报表生成以及数据交换等场景中非常常见。...
这本书的源代码涵盖了C#语言的基础到进阶内容,包括变量、控制流、函数、类和对象、数据结构、异常处理、文件和数据库操作等多个方面。 1. 变量与数据类型:在C#中,变量是存储数据的容器。源代码会展示如何声明和...
在C#开发中,将DataTable转换为List泛型集合是一个常见的需求,特别是在使用三层架构进行网站开发时,我们可能会从数据访问层获取一个DataTable对象,然后需要将其转换为一个强类型的List集合以便在业务逻辑层或表示...
### C# 中 DataTable 数据到 Excel 的导出及行列转换 #### 一、概述 在实际开发过程中,我们经常需要将数据库中的数据导出为 Excel 文件,以便于数据分析或分享给其他人。同时,在某些场景下还需要对数据进行行列...
在C#编程中,连接和操作数据库是常见的任务,尤其在开发桌面应用、Web应用或者服务时。本篇文章将详细讲解如何使用C#语言连接不同的数据库,并通过执行SQL语句获取DataTable对象,其中包括Access、SQL Server、...
根据DataTable中数据行记录改变的状态,动态生成SQL语句, 如果一个表中字段上百个,在只改了其中两三个字段时,则生成的SQL语句就只会含有这修改内容的字段, 有了这一个函数,从此彻底告别手动写SQL语句更新到...
根据提供的文件信息,本文将详细解释C#语言中如何实现对Excel文件的操作,包括但不限于将数据表保存到Excel、从Excel加载数据集等操作。这些技术对于开发人员来说非常重要,尤其是在处理大量数据或进行数据分析时。 ...
彻底告别手动写SQL语句,一劳永逸解决数据库字段变更后代码也需要随之修改的问题。 根据DataTable中数据行记录改变的状态,动态生成SQL语句, 如果一个表中字段上百个,在只改了其中两三个字段时,则生成的SQL语句...
在.NET框架中,C#是一种常用的编程语言,用于构建各种应用程序。`DataTable`是System.Data命名空间中的一个类,它是数据库操作中常见的数据结构,它允许我们存储和处理表格形式的数据,无需直接与数据库交互。在C#中...