Created
March 9, 2012 19:39
-
-
Save achvaicer/2008275 to your computer and use it in GitHub Desktop.
Classe para executar procedures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Data.SqlClient; | |
using System.Configuration; | |
using System.Data; | |
namespace Infra | |
{ | |
public class ProcedureExecuter | |
{ | |
private static SqlTransaction _transaction; | |
private static SqlConnection _connection; | |
private static bool EnsureConnectionIsOpened() | |
{ | |
if (_connection == null || _connection.State != System.Data.ConnectionState.Open) | |
{ | |
CloseConnection(); | |
_connection = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString); | |
_connection.Open(); | |
return true; | |
} | |
return false; | |
} | |
private static void CloseConnection() | |
{ | |
CloseTransaction(); | |
if (_connection == null) return; | |
_connection.Close(); | |
_connection.Dispose(); | |
} | |
private static void CloseTransaction() | |
{ | |
if (_transaction == null) return; | |
_transaction.Dispose(); | |
} | |
public static void BeginTransaction() | |
{ | |
CloseConnection(); | |
EnsureConnectionIsOpened(); | |
_transaction = _connection.BeginTransaction(); | |
} | |
public static void Commit() | |
{ | |
if (_transaction != null) | |
_transaction.Commit(); | |
CloseConnection(); | |
} | |
public static void Rollback() | |
{ | |
if (_transaction != null) | |
_transaction.Rollback(); | |
CloseConnection(); | |
} | |
public static object ExecuteScalar(string storeProcedureName, Dictionary<string, object> parameters) | |
{ | |
object ret; | |
var justopened = EnsureConnectionIsOpened(); | |
using (var cmd = (_transaction != null && _transaction.Connection != null ? _transaction.Connection : _connection).CreateCommand()) | |
{ | |
PrepareCommand(storeProcedureName, parameters, _transaction, cmd); | |
ret = cmd.ExecuteScalar(); | |
} | |
if (justopened && _transaction == null) | |
CloseConnection(); | |
return ret; | |
} | |
public static IEnumerable<T> ExecuteDataReader<T>(string storeProcedureName, Dictionary<string, object> parameters) where T : new() | |
{ | |
IList<T> list = new List<T>(); | |
var justopened = EnsureConnectionIsOpened(); | |
var modelType = typeof(T); | |
using (var cmd = (_transaction != null && _transaction.Connection != null ? _transaction.Connection : _connection).CreateCommand()) | |
{ | |
PrepareCommand(storeProcedureName, parameters, _transaction, cmd); | |
using (SqlDataReader reader = cmd.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
var item = ReaderToDomainObject<T>(modelType, reader); | |
list.Add(item); | |
} | |
} | |
} | |
if (justopened && _transaction == null) | |
CloseConnection(); | |
return list; | |
} | |
private static T ReaderToDomainObject<T>(Type modelType, SqlDataReader reader) where T : new() | |
{ | |
var item = new T(); | |
var properties = GetFieldNames(reader); | |
foreach (var prop in properties) | |
{ | |
SetPropertyValue<T>(modelType, reader, item, prop); | |
} | |
return item; | |
} | |
private static void SetPropertyValue<T>(Type modelType, SqlDataReader reader, T item, string prop) where T : new() | |
{ | |
var o = reader[prop]; | |
var type = o.GetType(); | |
if (type != typeof(DBNull)) | |
{ | |
var p = modelType.GetProperty(prop); | |
if (p != null) | |
p.SetValue(item, o, null); | |
} | |
} | |
private static IEnumerable<string> GetFieldNames(IDataRecord dataRecord) | |
{ | |
for (int i = 0; i < dataRecord.FieldCount; i++) | |
yield return dataRecord.GetName(i); | |
} | |
private static void PrepareCommand(string storeProcedureName, Dictionary<string, object> parameters, SqlTransaction trans, SqlCommand cmd) | |
{ | |
cmd.Transaction = trans; | |
DictionaryToParameters(parameters, cmd); | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
cmd.CommandText = storeProcedureName; | |
} | |
private static void DictionaryToParameters(Dictionary<string, object> parameters, SqlCommand cmd) | |
{ | |
if (parameters == null) return; | |
foreach (var parameter in parameters) | |
cmd.Parameters.AddWithValue(parameter.Key, parameter.Value); | |
} | |
} | |
} |
Nem fala!
No meu trabalho qualquer acesso ao banco é via procedure.
Usar qualquer orm é horrível. NHibernate, EF, Simple.Data, LinqToSQL.
Resolvi fazer minha solulção pra executar as procedures de forma fácil.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Caramba, tá nesse nível? :P