From d164fb4092c24ac49e94b39d6ed8e0a6e690d9f9 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Fri, 1 Apr 2016 17:29:54 -0700 Subject: [PATCH] Issue #11: Added support for returning a DBDataReader based on xavierjohn's suggestion --- .../DatabaseExtensions.cs | 23 +++++++++++++++++++ .../DatabaseExtensions.cs | 23 +++++++++++++++++++ EntityFrameworkExtras/DatabaseExtensions.cs | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/EntityFrameworkExtras.EF5/DatabaseExtensions.cs b/EntityFrameworkExtras.EF5/DatabaseExtensions.cs index bf47d95..505b162 100644 --- a/EntityFrameworkExtras.EF5/DatabaseExtensions.cs +++ b/EntityFrameworkExtras.EF5/DatabaseExtensions.cs @@ -52,6 +52,29 @@ public static IEnumerable ExecuteStoredProcedure(this Database database, o return result; } + /// + /// Executes the specified stored procedure against a database + /// and returns a DbDataReader. Used for procedures returning multiple result sets. + /// + /// The database to execute against. + /// The stored procedure to execute. + /// + public static System.Data.Common.DbDataReader ExecuteReader(this Database database, object storedProcedure) + { + if (storedProcedure == null) + throw new ArgumentNullException("storedProcedure"); + + var info = StoredProcedureParser.BuildStoredProcedureInfo(storedProcedure); + + using (var cmd = database.Connection.CreateCommand()) + { + cmd.CommandText = info.Sql; + cmd.Parameters.AddRange(info.SqlParameters); + + return cmd.ExecuteReader(); + } + } + private static void SetOutputParameterValues(IEnumerable sqlParameters, object storedProcedure) { foreach (SqlParameter sqlParameter in sqlParameters.Where(p => p.Direction != ParameterDirection.Input)) diff --git a/EntityFrameworkExtras.EF6/DatabaseExtensions.cs b/EntityFrameworkExtras.EF6/DatabaseExtensions.cs index 55b3140..664da04 100644 --- a/EntityFrameworkExtras.EF6/DatabaseExtensions.cs +++ b/EntityFrameworkExtras.EF6/DatabaseExtensions.cs @@ -52,6 +52,29 @@ public static IEnumerable ExecuteStoredProcedure(this Database database, o return result; } + /// + /// Executes the specified stored procedure against a database + /// and returns a DbDataReader. Used for procedures returning multiple result sets. + /// + /// The database to execute against. + /// The stored procedure to execute. + /// + public static System.Data.Common.DbDataReader ExecuteReader(this Database database, object storedProcedure) + { + if (storedProcedure == null) + throw new ArgumentNullException("storedProcedure"); + + var info = StoredProcedureParser.BuildStoredProcedureInfo(storedProcedure); + + using (var cmd = database.Connection.CreateCommand()) + { + cmd.CommandText = info.Sql; + cmd.Parameters.AddRange(info.SqlParameters); + + return cmd.ExecuteReader(); + } + } + private static void SetOutputParameterValues(IEnumerable sqlParameters, object storedProcedure) { foreach (SqlParameter sqlParameter in sqlParameters.Where(p => p.Direction != ParameterDirection.Input)) diff --git a/EntityFrameworkExtras/DatabaseExtensions.cs b/EntityFrameworkExtras/DatabaseExtensions.cs index be63f47..e61e40e 100644 --- a/EntityFrameworkExtras/DatabaseExtensions.cs +++ b/EntityFrameworkExtras/DatabaseExtensions.cs @@ -52,6 +52,29 @@ public static IEnumerable ExecuteStoredProcedure(this Database database, o return result; } + /// + /// Executes the specified stored procedure against a database + /// and returns a DbDataReader. Used for procedures returning multiple result sets. + /// + /// The database to execute against. + /// The stored procedure to execute. + /// + public static System.Data.Common.DbDataReader ExecuteReader(this Database database, object storedProcedure) + { + if (storedProcedure == null) + throw new ArgumentNullException("storedProcedure"); + + var info = StoredProcedureParser.BuildStoredProcedureInfo(storedProcedure); + + using (var cmd = database.Connection.CreateCommand()) + { + cmd.CommandText = info.Sql; + cmd.Parameters.AddRange(info.SqlParameters); + + return cmd.ExecuteReader(); + } + } + private static void SetOutputParameterValues(IEnumerable sqlParameters, object storedProcedure) { foreach (SqlParameter sqlParameter in sqlParameters.Where(p => p.Direction != ParameterDirection.Input))