The following class has several methods that help working with DataSet in projects
a lot easier.
There are situations like,
1. You need to decide whether or not to compress a DataSet depending on the size
2. Retrieve the errors that are created due to Constraints
3. Selecting duplicate values in a DataColumn
4. Selecting unique values in a DataColumn
5. Avoid common "Object Not set to reference of a object error", errors,
due to non-check of (dataSet == null)
or due a null DataTable
and much more
The following class address these.
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
namespace Utilities
{
/// <summary>
/// DataSet Extensions.
/// </summary>
public static class DataSetExtensions
{
/// <summary>
/// Selects the duplicates.
/// </summary>
/// <param name="dataTable">The data table.</param>
/// <param name="columnName">Name of the column.</param>
/// <returns></returns>
public static List<object> SelectDuplicates(this DataTable dataTable, string columnName)
{
List<object> duplicates = new List<object>();
for (int iValue = 0; iValue < dataTable.Rows.Count; iValue++)
{
object lastValue = dataTable.Rows[iValue][columnName];
if (duplicates.Contains(lastValue))
duplicates.Add(lastValue);
}
return duplicates;
}
/// <summary>
/// Selects the uniques.
/// </summary>
/// <param name="dataTable">The data table.</param>
/// <param name="columnName">Name of the column.</param>
/// <returns></returns>
public static List<object> SelectUniques(this DataTable dataTable, string columnName)
{
List<object> uniques = new List<object>();
for (int iValue = 0; iValue < dataTable.Rows.Count; iValue++)
{
object lastValue = dataTable.Rows[iValue][columnName];
if (!uniques.Contains(lastValue))
uniques.Add(lastValue);
}
return uniques;
}
/// <summary>
/// Gets the tables count.
/// </summary>
/// <param name="dataSet">The data set.</param>
/// <returns></returns>
public static int GetTablesCount(this DataSet dataSet)
{
if (!dataSet.IsEmpty())
return dataSet.Tables.Count;
else
return 0;
}
/// <summary>
/// Determines whether the specified data set is empty.
/// </summary>
/// <param name="dataSet">The data set.</param>
/// <returns>
/// <c>true</c> if the specified data set is
empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsEmpty(this DataSet dataSet)
{
bool isEmpty = false;
if (dataSet == null)
{
isEmpty
= true;
}
else
{
if (dataSet.Tables.Count == 0)
{
isEmpty
= true;
}
else
{
isEmpty
= dataSet.Tables[0].Rows.Count == 0 ? true : false;
}
}
return isEmpty;
}
/// <summary>
/// Determines whether the specified data set has constraints.
/// </summary>
/// <param name="dataSet">The data set.</param>
/// <param name="tableName">Name of the table.</param>
/// <returns>
/// <c>true</c> if the specified data set has
constraints; otherwise, <c>false</c>.
/// </returns>
public static bool HasConstraints(this DataSet dataSet, string tableName)
{
return (dataSet.Tables[tableName].Constraints.Count > 0 ? true : false);
}
/// <summary>
/// Determines whether the specified data set has errors.
/// </summary>
/// <param name="dataSet">The data set.</param>
/// <param name="tableName">Name of the table.</param>
/// <returns>
/// <c>true</c> if the specified data set has
errors; otherwise, <c>false</c>.
/// </returns>
public static bool HasTableErrors(this DataSet dataSet, string tableName)
{
return (dataSet.Tables[tableName].HasErrors);
}
/// <summary>
/// Gets the table errors.
/// </summary>
/// <param name="dataset">The dataset.</param>
/// <param name="tableName">Name of the table.</param>
/// <returns></returns>
public static string GetTableErrors(this DataSet dataset, string tableName)
{
string error = string.Empty;
if (dataset.HasTableErrors(tableName))
{
foreach (DataRow row in dataset.Tables[tableName].GetErrors())
{
foreach (DataColumn column in row.GetColumnsInError())
{
error
+= string.Format("Error : {0}"
,
row.GetColumnError(column));
}
}
}
return error;
}
/// <summary>
/// Gets the data set sizein bytes.
/// </summary>
/// <param name="dataSet">The data set.</param>
/// <returns></returns>
public static int GetDataSetSizeinBytes(this DataSet dataSet)
{
if (dataSet != null)
{
MemoryStream
stream = new MemoryStream();
BinaryFormatter
binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream,
dataSet);
stream.Seek(0,
0);
byte[] result = stream.ToArray();
stream.Close();
return result.Length;
}
else
return 0;
}
}
}
Cheers.