From 02bec7adab522ac7a7a2fff62d592878fbb532c5 Mon Sep 17 00:00:00 2001 From: David Crafti Date: Tue, 22 Mar 2016 12:42:44 +1100 Subject: [PATCH] Using a clone to prevent an error if the param is a SqlParameter, rather than a value type. The error is "The SqlParameter is already contained by another SqlParameterCollection" --- PetaPoco/Utilities/ParametersHelper.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/PetaPoco/Utilities/ParametersHelper.cs b/PetaPoco/Utilities/ParametersHelper.cs index ea7c5aa3..196aeb5a 100644 --- a/PetaPoco/Utilities/ParametersHelper.cs +++ b/PetaPoco/Utilities/ParametersHelper.cs @@ -68,11 +68,16 @@ public static string ProcessParams(string sql, object[] args_src, List a } else { - args_dest.Add(arg_val); + var argValCopy = arg_val; + if (argValCopy is ICloneable) + { // Using a clone prevents the error "The SqlParameter is already contained by another SqlParameterCollection" if the parameter is a SqlParameter, rather than a value type. + argValCopy = ((ICloneable)argValCopy).Clone(); + } + args_dest.Add(argValCopy); return "@" + (args_dest.Count - 1).ToString(); } } ); } } -} \ No newline at end of file +}