From f43e2dca93faeba8fa54a572962be0311f3d3adf Mon Sep 17 00:00:00 2001 From: "a.bozhenov" Date: Fri, 30 Aug 2019 21:42:32 +0300 Subject: [PATCH] ext --- ZeroLevel.SQL/Contracts/IDbProvider.cs | 1 + ZeroLevel.SQL/SqlDbProvider.cs | 32 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ZeroLevel.SQL/Contracts/IDbProvider.cs b/ZeroLevel.SQL/Contracts/IDbProvider.cs index 7bcbf13..c30e62f 100644 --- a/ZeroLevel.SQL/Contracts/IDbProvider.cs +++ b/ZeroLevel.SQL/Contracts/IDbProvider.cs @@ -17,6 +17,7 @@ namespace ZeroLevel.SqlServer void ExecuteNonResult(IEnumerable commands); int ExecuteNonResult(string query); int ExecuteNonResult(string query, DbParameter[] par); + DbReader ExecuteReader(string query, DbParameter[] par); void LazySelect(string query, DbParameter[] par, Func readHandler, int timeout); T Read(DbDataReader reader, int index); diff --git a/ZeroLevel.SQL/SqlDbProvider.cs b/ZeroLevel.SQL/SqlDbProvider.cs index 2c93c6b..a74f1ea 100644 --- a/ZeroLevel.SQL/SqlDbProvider.cs +++ b/ZeroLevel.SQL/SqlDbProvider.cs @@ -6,6 +6,31 @@ using System.Data.SqlClient; namespace ZeroLevel.SqlServer { + public class DbReader + :IDisposable + { + private readonly DbConnection _connection; + private readonly IDbCommand _command; + + public DbReader(DbConnection connection, IDbCommand command) + { + _connection = connection; + _command = command; + } + + public IDataReader GetReader() + { + return _command.ExecuteReader(CommandBehavior.CloseConnection); + } + + public void Dispose() + { + _command.Dispose(); + _connection.Close(); + _connection.Dispose(); + } + } + public class SqlDbProvider : IDbProvider { @@ -185,6 +210,13 @@ namespace ZeroLevel.SqlServer } #endregion + public DbReader ExecuteReader(string query, DbParameter[] par) + { + var connection = _factory.CreateConnection(); + return new DbReader(connection, CreateCommand(connection, query, par, Timeout)); + + } + #region LazySelect public void LazySelect(string query, DbParameter[] par, Func readHandler, int timeout = Timeout) {