Created
June 20, 2011 18:13
-
-
Save einarwh/1036183 to your computer and use it in GitHub Desktop.
Dry data: StoredProcedure class (aka "the parameter sponge")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StoredProcedure | |
{ | |
private readonly DbProviderFactory _dpf; | |
private readonly DbCommand _sp; | |
public StoredProcedure(DbCommand sp, DbProviderFactory dpf) | |
{ | |
_sp = sp; | |
_dpf = dpf; | |
} | |
public StoredProcedure this[string parameterName, | |
object value, int? size = null, DbType? type = null] | |
{ | |
get { return AddParameter(parameterName, value, size, type); } | |
} | |
public StoredProcedure AddParameter(string parameterName, | |
object value, int? size = null, DbType? type = null) | |
{ | |
var p = _dpf.CreateParameter(); | |
if (p != null) | |
{ | |
p.ParameterName = parameterName; | |
p.Value = value; | |
if (size.HasValue) | |
{ | |
p.Size = size.Value; | |
} | |
if (type.HasValue) | |
{ | |
p.DbType = type.Value; | |
} | |
_sp.Parameters.Add(p); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment