c# - 通过 ODBC 传递 null

标签 c# db2 odbc

我在 DB2 中有一个存储过程,它接受一个 Date 作为参数。 我使用这段代码在 C# 中为其赋值

cmd.Parameters.Add("myfield", OdbcType.DateTime).Value = MyDate;

这很好用!但是有时我想将它传递给 null 而我无法让它工作。 我试过了

cmd.Parameters.Add("myfield", OdbcType.DateTime);

cmd.Parameters.Add("myfield", OdbcType.DateTime).Value =DBNull.Value

更新

我的 cmd.CommandText = "{ CALL foo.MY_PROC_NAME(?) }"

当我运行它时,它没有返回任何行。 我在 SP 中的 SQL 看起来像这样

(p_myfield is null or LAST_UPDATE_DATE > p_myfield) 

最佳答案

像这样的东西应该适用于可为 null 的日期时间参数

DateTime? myfield

if (null==myfield)
{
 var nullableparam = new OdbcParameter("@myfield", DBNull.Value);
 nullableparam.IsNullable = true;
 nullableparam.OdbcType = OdbcType.DateTime;
 nullableparam.Direction = ParameterDirection.Input;
 cm.Parameters.Add(nullableparam);
}
else
{
 cm.Parameters.AddwithValue("@myfield",myfield);
}

关于c# - 通过 ODBC 传递 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16808643/

相关文章:

c++ - 无法在 Apache Ignite 中安装 ODBC 驱动程序 C++

c# - 将单词(字符串)转换为 Int

c# - XAML 中图标格式的最佳实践(SVG、路径数据点、几何/XAML 或字体)

c# - 服务层只返回模型

c# - 如何使用 HtmlHelper 创建外部超链接?

hadoop - DB2数据导入hadoop

database - DB2 操作超时或死锁

java - JAR 文件无法识别 mysql 数据库

encryption - 使用明文 key 进行 3DES 加密

python - 使用 pyodbc 从 Python 到 Access 2016 的 ODBC 连接失败