仅供自己记录复习
#region DataTable
/// <summary>
/// 判断DS是否为空
/// </summary>
/// <param name="ds">需要判断的ds</param>
/// <returns>如果ds为空,返回true</returns>
private bool JudgeDs(DataSet ds)
{
bool Flag = false;
if ((ds == null) || (ds.Tables.Count == 0) || (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
{
Flag = true;
}
return Flag;
}
/// <summary>
/// 检查一个DataSet 里面是否含有数据
/// </summary>
/// <param name="ds">要检测的DataSet</param>
/// <param name="tableIndex">DataSet里Table的索引</param>
/// <returns>True: 里面有数据。 False:里面没有数据</returns>
public static bool IfExitData(DataSet ds, int tableIndex)
{
if (ds != null && ds.Tables[tableIndex].Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// DataTable去掉重复数据(重复列)
/// </summary>
/// <param name="dtSource"></param>
/// <param name="columnNames"></param>
/// <returns></returns>
public static DataTable GetDistinctTable(DataTable dtSource, params string[] columnNames)
{
DataTable distinctTable = dtSource.Clone();
try
{
if (dtSource != null && dtSource.Rows.Count > 0)
{
DataView dv = new DataView(dtSource);
distinctTable = dv.ToTable(true, columnNames);
}
}
catch (Exception ee)
{
// MessageBox.Show(ee.ToString());
}
return distinctTable;
}
public static DataTable GetDistinctTable(DataTable dtSource)
{
DataTable distinctTable = null;
try
{
if (dtSource != null && dtSource.Rows.Count > 0)
{
string[] columnNames = GetTableColumnName(dtSource);
DataView dv = new DataView(dtSource);
distinctTable = dv.ToTable(true, columnNames);
}
}
catch (Exception ee)
{
}
return distinctTable;
}
/// <summary>
/// 获取所有列名
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string[] GetTableColumnName(DataTable dt)
{
string cols = string.Empty;
for (int i = 0; i < dt.Columns.Count; i++)
{
cols += (dt.Columns[i].ColumnName + ",");
}
cols = cols.TrimEnd(',');
return cols.Split(',');
}
/// <summary>
/// 多个空格为一个空格
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string MergeSpace(string str)
{
if (str != string.Empty &&
str != null &&
str.Length > 0
)
{
str = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(str, " ");
}
return str;
}
/// <summary>
/// 比较两个DataTable内容是否相等,先是比数量,数量相等就比内容
/// </summary>
/// <param name= "dtA "> </param>
/// <param name= "dtB "> </param>
public static bool CompareDataTable(DataTable dtA, DataTable dtB)
{
if (dtA.Rows.Count == dtB.Rows.Count)
{
if (CompareColumn(dtA.Columns, dtB.Columns))
{
//比内容
for (int i = 0; i < dtA.Rows.Count; i++)
{
for (int j = 0; j < dtA.Columns.Count; j++)
{
if (!dtA.Rows[i][j].Equals(dtB.Rows[i][j]))
{
return false;
}
}
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 比较两个字段集合是否名称,数据类型一致
/// </summary>
/// <param name= "dcA "> </param>
/// <param name= "dcB "> </param>
/// <returns> </returns>
private static bool CompareColumn(System.Data.DataColumnCollection dcA, System.Data.DataColumnCollection dcB)
{
if (dcA.Count == dcB.Count)
{
foreach (DataColumn dc in dcA)
{
//找相同字段名称
if (dcB.IndexOf(dc.ColumnName) > -1)
{
//测试数据类型
if (dc.DataType != dcB[dcB.IndexOf(dc.ColumnName)].DataType)
{
return false;
}
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
#endregion