DbNull.Value和DbNull.Value.ToString()之间的区别 [英] Difference between DbNull.Value and DbNull.Value.ToString()

查看:153
本文介绍了DbNull.Value和DbNull.Value.ToString()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解哪种用法是正确的?

I wanted to learn which usage is true?

if(!string.IsNullOrEmpty(parentID))
   cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));
else
   cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value));

OR

if(!string.IsNullOrEmpty(parentID))
   cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));
else
   cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString()));


推荐答案

cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));

这会将parentID传递给参数@ParentSesID。

This is passing a parentID to parameter @ParentSesID.

cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value));

null 值传递给参数。

cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString()));

通过等于 string.Empty

cmd.Parameters.Add(new SqlParameter("@ParentSesID", null);

与忽略参数相同。

因此,当您需要将 null 传递给SP时,您必须传递 DBNull.Value

So when you need to pass null to SP you've to pass DBNull.Value.

这篇关于DbNull.Value和DbNull.Value.ToString()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆