From 22888dda16559868bcff7336edabd33aec8a1b54 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Fri, 1 Apr 2016 17:14:27 -0700 Subject: [PATCH] Issue #11: Added support for returning a DBDataReader based on xavierjohn's suggestion --- .../DatabaseExtensions.cs | 21 +++++++++++++++++++ .../DatabaseExtensions.cs | 21 +++++++++++++++++++ EntityFrameworkExtras/DatabaseExtensions.cs | 21 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/EntityFrameworkExtras.EF5/DatabaseExtensions.cs b/EntityFrameworkExtras.EF5/DatabaseExtensions.cs index bf47d95..3ea1a7c 100644 --- a/EntityFrameworkExtras.EF5/DatabaseExtensions.cs +++ b/EntityFrameworkExtras.EF5/DatabaseExtensions.cs @@ -52,6 +52,27 @@ 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); + + 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..0c9f3cf 100644 --- a/EntityFrameworkExtras.EF6/DatabaseExtensions.cs +++ b/EntityFrameworkExtras.EF6/DatabaseExtensions.cs @@ -52,6 +52,27 @@ 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); + + 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..63c11d2 100644 --- a/EntityFrameworkExtras/DatabaseExtensions.cs +++ b/EntityFrameworkExtras/DatabaseExtensions.cs @@ -52,6 +52,27 @@ 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); + + 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))