namespace HW.Utility.Data
|
{
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Data.Common;
|
using HW.Utility;
|
|
public static class DataAccessExtensive
|
{
|
public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd)
|
{
|
return ExecuteDataTable(dataAccess, cmd, null);
|
}
|
|
public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd, DbTransaction trans)
|
{
|
DataTable table = new DataTable();
|
DataSet set = (trans != null) ? dataAccess.ExecuteDataSet(cmd, trans) : dataAccess.ExecuteDataSet(cmd);
|
if ((set != null) && (set.Tables.Count > 0))
|
{
|
table = set.Tables[0];
|
}
|
return table;
|
}
|
|
public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T: class, new()
|
{
|
return ExecuteListEntity<T>(dataAccess, cmd, action, null);
|
}
|
|
public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T: class, new()
|
{
|
List<T> list = new List<T>();
|
if (action != null)
|
{
|
using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
|
{
|
while (reader.Read())
|
{
|
T local = Activator.CreateInstance<T>();
|
action(local, reader);
|
list.Add(local);
|
}
|
}
|
}
|
return list;
|
}
|
|
public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd)
|
{
|
return ExecuteSingleData(dataAccess, cmd, null);
|
}
|
|
public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd, DbTransaction trans)
|
{
|
DataRow row = null;
|
DataTable table = ExecuteDataTable(dataAccess, cmd, trans);
|
if ((table != null) && (table.Rows.Count > 0))
|
{
|
row = table.Rows[0];
|
}
|
return row;
|
}
|
|
public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T: class, new()
|
{
|
return ExecuteSingleEntity<T>(dataAccess, cmd, action, null);
|
}
|
|
public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T: class, new()
|
{
|
T local = default(T);
|
if (action != null)
|
{
|
using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
|
{
|
if (reader.Read())
|
{
|
local = Activator.CreateInstance<T>();
|
action(local, reader);
|
}
|
}
|
}
|
return local;
|
}
|
|
public static bool ExecuteTransaction(Database dataAccess, TransactionAction action, Action<Exception> logAction)
|
{
|
bool flag = false;
|
if (action != null)
|
{
|
using (DbConnection connection = dataAccess.CreateConnection())
|
{
|
connection.Open();
|
DbTransaction trans = connection.BeginTransaction();
|
try
|
{
|
if (action(trans))
|
{
|
trans.Commit();
|
}
|
else
|
{
|
trans.Rollback();
|
}
|
flag = true;
|
}
|
catch (Exception exception)
|
{
|
if (logAction != null)
|
{
|
logAction(exception);
|
}
|
trans.Rollback();
|
throw exception;
|
}
|
connection.Close();
|
}
|
}
|
return flag;
|
}
|
}
|
}
|