I've written a utility to handle adding values to data parameters for hand-rolling sql queries. Consumption looks like
utility.Add("SELECT * FROM MyTable WHERE MyColumn = {0}", myVariable, DBType.TheType);
or
utility.Add("UPDATE MyTable SET MyColumn = {0}", myVariable, DBType.TheType);
How should a null value for myVariable be handled?
Optional bonus question: If these arguments were made by three political candidates, what would their parties be? (Please state the home country of such parties)
Rock the Null Coalescing Op:
In your add put another parm for the =... utility.Add("SELECT * FROM MyTable WHERE MyColumn {0}{1}", myVariable, DBType.TheType);
When you format the string in the Add method: ... = String.Format(sql,myVariable is null ? " IS " : " = ", myVariable ?? "Null"
You'll probably want to expound on that a bit, e.g., stick with on parameter and toss in a temp stringbuilder; might have to use a the ?/: construct for the type, too, wherever that's being used. Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With