我在 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/