DbParameter IsNullable 功能?
我很少使用 ADO.Net 中的参数。 我正在编写一个自定义 .Net 数据提供程序(以 SqlClient 为模型),并且必须在继承自 DbParameter 的参数类中实现 IsNullable 属性。 我的数据提供程序不支持存储过程,因此我仅支持输入(替换样式)参数。
MSDN 文档对 IsNullable 的功能相当不清楚,指出“获取或设置一个指示参数是否接受空值的值”。 谷歌搜索发现很多人对 IsNullable 的作用感到困惑,他们说将 IsNullable 设置为 false 并不会阻止他们使用空值参数,正如他们所期望的那样。
基于此,我认为 IsNullable 属性可能与存储过程的使用有关,以及存储过程的参数是否可为空,而不是参数值是否可以为空。
在这种情况下,由于我不会有存储过程,所以我的实现可以是:
public override bool IsNullable { get { return false; } set {} }
为了避免必须摆弄一些测试代码来查看 SqlParameter 的 IsNullable 实现如何工作,如果有人有使用编写代码的经验,我将不胜感激参数可以解释 IsNullable。
I have not worked with parameters in ADO.Net very much. I'm writing a custom .Net data provider (modeled on SqlClient), and have to implement the IsNullable property in my parameter class, which inherits from DbParameter. My data provider will not support stored procedures, so I will only be supporting Input (substitution style) parameters.
The MSDN docs are rather unclear on the functionality of IsNullable, stating "Gets or sets a value that indicates whether the parameter accepts null values." Googling turns up a lot of people confused about what IsNullable does, saying that setting IsNullable to false does not prohibit them from using a parameter will a null value, as they would have expected.
Based on this, I am thinking that perhaps the IsNullable property pertains to usage with a stored procedure, and whether or not the stored procedure's parameter is nullable, not whether the parameter value can be null.
In that case, since I won't have stored procedures, my implementation can just be:
public override bool IsNullable { get { return false; } set {} }
To avoid having to fiddle around with some test code to see how SqlParameter's implementation of IsNullable works, I'd appreciate it if someone experienced in writing code using parameters could explain IsNullable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IsNullable 属性是信息性的。 当代码的一部分创建参数而另一部分实际使用它时,这会很有帮助。 例如,参数可能会自动从数据库中派生,在这种情况下,程序员能够了解是否可能接受空值会很有帮助。
这是我对 IsNullable 属性的理解。 我实际上不知道有任何文档可以“证明”这实际上是正确的。
支持此观点的是 Oracle
Oracle.DataAccess.Client.OracleParameter
类,其文档指出:事实上,当通过 MS 驱动程序或引用的 Oracle 驱动程序与 Oracle 10、11 或 12 连接时,此属性的值是无关紧要的。
当然,其他数据库上的行为可能有所不同。
The IsNullable property is informational. It is helpful when one part of code creates the parameter and another actually uses it. For example, the parameter might be automatically derived from the database, in which case it is helpful to the programmer to be able to see whether or not a null value is likely to be accepted.
This is my understanding of the IsNullable property. I don't actually know of any documentation that "proves" this is actually correct.
Supporting this view would be the implementation of the Oracle
Oracle.DataAccess.Client.OracleParameter
class, where their docs state:And it is a fact that the value of this property is irrelevant when interfacing with Oracle 10 or 11 or 12, either via the MS driver or the cited Oracle driver.
Behavior on other databases may differ of course.