diff --git a/CodeOnlyStoredProcedure/StoredProcedureExtensions.WithTableValuedParameter.cs b/CodeOnlyStoredProcedure/StoredProcedureExtensions.WithTableValuedParameter.cs index 5b4f2a5..282bc65 100644 --- a/CodeOnlyStoredProcedure/StoredProcedureExtensions.WithTableValuedParameter.cs +++ b/CodeOnlyStoredProcedure/StoredProcedureExtensions.WithTableValuedParameter.cs @@ -89,6 +89,30 @@ public static TSP WithTableValuedParameter(this TSP sp, return (TSP)sp.CloneWith(new TableValuedParameter(name, table, typeof(TRow), tableTypeName, tableTypeSchema)); } + /// + /// Clones the given , and associates the items + /// as a Table Valued Parameter. + /// + /// The type of to associate the Table Valued Parameter with. + /// The to clone. + /// The name of the Table Valued Parameter in the stored procedure. + /// The to pass in the Table Valued Parameter. Make sure the is set, + /// as that will be used to identify the TVP's type in the database. + /// A copy of the that has the Table Valued Parameter set. + /// StoredProcedures are immutable, so all the Fluent API methods return copies. + public static TSP WithTableValuedParameter(this TSP sp, + string name, + DataTable table) + where TSP : StoredProcedure + { + Contract.Requires(sp != null); + Contract.Requires(!string.IsNullOrWhiteSpace(name)); + Contract.Requires(table != null); + Contract.Ensures(Contract.Result() != null); + + return (TSP)sp.CloneWith(new TableValuedParameter(name, table)); + } + /// /// Clones the given , and associates the items /// as a Table Valued Parameter. diff --git a/CodeOnlyStoredProcedure/StoredProcedureParameters/TableValuedParameter.cs b/CodeOnlyStoredProcedure/StoredProcedureParameters/TableValuedParameter.cs index e345dc0..a767b8b 100644 --- a/CodeOnlyStoredProcedure/StoredProcedureParameters/TableValuedParameter.cs +++ b/CodeOnlyStoredProcedure/StoredProcedureParameters/TableValuedParameter.cs @@ -25,6 +25,9 @@ public TableValuedParameter(string name, DataTable data) Contract.Requires(!string.IsNullOrWhiteSpace(name)); Contract.Requires(data != null); + if (string.IsNullOrWhiteSpace(data.TableName)) + throw new NotSupportedException("When passing a DataTable, either set its TypeName to the TVP's type, or pass it in as one of the parameters."); + ParameterName = name; this.data = data; this.TypeName = data.TableName; diff --git a/CodeOnlyStoredProcedures.nuspec b/CodeOnlyStoredProcedures.nuspec index ce53b43..e0587b5 100644 --- a/CodeOnlyStoredProcedures.nuspec +++ b/CodeOnlyStoredProcedures.nuspec @@ -12,6 +12,7 @@ Code Only Stored Procedures will not create any Stored Procedures on your databa 2.4.0 Added support for binary blobs for both results and parameters by using a byte array. Fixes bug where output parameters can not be used if the stored procedure returns results. +Added support for using DataTable to pass a Table-Valued Parameter 2.3.0 Can now opt in to not clone the database connection before executing a StoredProcedure.