如何清晰快速地使用 DBNull.Value 参数化空字符串
我厌倦了编写以下代码:
/* Commenting out irrelevant parts
public string MiddleName;
public void Save(){
SqlCommand = new SqlCommand();
// blah blah...boring INSERT statement with params etc go here. */
if(MiddleName==null){
myCmd.Parameters.Add("@MiddleName", DBNull.Value);
}
else{
myCmd.Parameters.Add("@MiddleName", MiddleName);
}
/*
// more boring code to save to DB.
}*/
所以,我写了这样的:
public static object DBNullValueorStringIfNotNull(string value)
{
object o;
if (value == null)
{
o = DBNull.Value;
}
else
{
o = value;
}
return o;
}
// which would be called like:
myCmd.Parameters.Add("@MiddleName", DBNullValueorStringIfNotNull(MiddleName));
如果这是执行此操作的好方法,那么您建议使用什么作为方法名称? DBNullValueorStringIfNotNull 有点冗长和令人困惑。
我也对完全缓解这个问题的方法持开放态度。我很想这样做:
myCmd.Parameters.Add("@MiddleName", MiddleName==null ? DBNull.Value : MiddleName);
但这行不通,因为“Operator '??'不能应用于“string”和“System.DBNull”类型的操作数”。
如果需要的话,我可以使用 C# 3.5 和 SQL Server 2005。
I got tired of writing the following code:
/* Commenting out irrelevant parts
public string MiddleName;
public void Save(){
SqlCommand = new SqlCommand();
// blah blah...boring INSERT statement with params etc go here. */
if(MiddleName==null){
myCmd.Parameters.Add("@MiddleName", DBNull.Value);
}
else{
myCmd.Parameters.Add("@MiddleName", MiddleName);
}
/*
// more boring code to save to DB.
}*/
So, I wrote this:
public static object DBNullValueorStringIfNotNull(string value)
{
object o;
if (value == null)
{
o = DBNull.Value;
}
else
{
o = value;
}
return o;
}
// which would be called like:
myCmd.Parameters.Add("@MiddleName", DBNullValueorStringIfNotNull(MiddleName));
If this is a good way to go about doing this then what would you suggest as the method name? DBNullValueorStringIfNotNull is a bit verbose and confusing.
I'm also open to ways to alleviate this problem entirely. I'd LOVE to do this:
myCmd.Parameters.Add("@MiddleName", MiddleName==null ? DBNull.Value : MiddleName);
but that won't work because the "Operator '??' cannot be applied to operands of type 'string and 'System.DBNull'".
I've got C# 3.5 and SQL Server 2005 at my disposal if it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将您的任何一个值转换为
object
并且它将编译。Cast either of your values to
object
and it will compile.您可以使用
SqlString.Null
而不是DBNull.Value
来避免显式转换为object
:int、datetime 等都有相应的类型四。这是一个带有更多示例的代码片段:
You can avoid the explicit cast to
object
usingSqlString.Null
instead ofDBNull.Value
:There are corresponding types for int, datetime, and so forth. Here's a code snippet with a couple more examples:
就我个人而言,这就是我对扩展方法所做的事情(确保它进入静态类)
然后你就可以了
Personally this is what I would do with an extension method (make sure this goes into a static class)
Then you'd have
@David 感谢您的建议。下面的方法效果很好!
@David Thanks for your suggestion. The following method works great!
是的,我们都喜欢这样做
myCmd.Parameters.Add("@MiddleName", MiddleName ?? DBNull.Value);
。或者更好的是,让奇怪的 SqlClient 层了解添加参数时 CLRnull
应该映射到DBNull.Value
。不幸的是,.Net 类型系统关闭了第一个选项,而 SqlClient 的实现则关闭了第二个选项。我会使用众所周知的函数名称,例如 Coalesce 或IsNull。任何数据库开发人员只要从名字上就能立刻认出他们在做什么。
Yeap, we'd all love to do
myCmd.Parameters.Add("@MiddleName", MiddleName ?? DBNull.Value);
. Or better still, have the freakin' SqlClient layer understand that CLRnull
should be mapped toDBNull.Value
when adding a parameter. Unfortunately the .Net type system closes the first alternative, and the implementation of SqlClient closes the second.I'd go with a well known function name, like Coalesce or IsNull. Any DB developer will recognize what they do in an instant, from the name alone.
我宁愿给你两个完全不同的建议:
使用 ORM。有很多非侵入式 ORM 工具。
编写您自己的包装器来构建命令,并具有更清晰的界面。像这样的东西:
如果将
AddParameter
方法重命名为Add
,则可以以非常灵活的方式使用它:I'd rather give you two totally different suggestions:
Use an ORM. There are plenty of non-intrusive ORM tools.
Write your own wrapper for building commands, with a cleaner interface. Something like:
If you rename your
AddParameter
methods to justAdd
, you can use it in a very slick way:我建议使用可为空的属性而不是公共字段和“AddParameter”方法(不知道这段代码是否经过优化或正确,就在我的脑海中):
I would suggest using nullable properties instead of public fields and an 'AddParameter' method (don't know if this code is optimized or correct, just off the top of my head):