如何从数据表中检查空白值并插入存储过程 [英] How to check blank value from datatable and insert in a Stored Procedure

查看:69
本文介绍了如何从数据表中检查空白值并插入存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello There



我有一个C#应用程序,我必须在其中创建一个用于插入记录的存储过程。



目前我正在使用以下功能: -

 cmd.Parameters.Add( new  SqlParameter(  PO_Date,Convert.ToDateTime(转换) .ToString(mDT_PoHead.Rows [ 0 ] [  PO_Date ])==   null :Convert.ToString(mDT_PoHead.Rows [ 0 ] [  PO_Date])))); 





因此,如果数据表字段的空值为空那么NULL将传递给其他实际值传递。



但最后出现错误SqlDateTime Overflow(mDT_PoHead.Rows [0] [PO_Date]有一个空白值。)



需要建议

最好的问候

解决方案

在这里你可以有两个选项,

一种方式是使用可空类型,如下所示(从你的实际值替换DATA_TABLE_VALUE)



 DateTime?  value  = Convert.IsDBNull([DATA_TABLE_VALUE])?  null  :( DateTime?)[DATA_TABLE_VALUE]; 





第二选项,

由于DateTime是一个值类型(如 int ),因此无法为其分配Null值。在这种情况下,您可以使用 DateTime.MinValue

 DateTime.MaxValue 

代替。


< blockquote>

 If(Field [ ]。ToString()! =  )值= Conver.ToDateTime(字段[  ])






设置参数属性

 boolIsNullable =  true  

创建新的sqlparameter时。


Hello There

I have a C# application in which I have to create a stored procedure for Inserting records.

Currently I am using below function:-

cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.ToDateTime(Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]) == "" ? null : Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]))));



So if the datatable field has a blank value then NULL will be passed else actual value pass.

But at the end error arise "SqlDateTime Overflow" (mDT_PoHead.Rows[0]["PO_Date"] has a blank value.)

Need Suggestion
Best Regards

解决方案

in here you can have two option,
one way is using nullable type, as below (replace the DATA_TABLE_VALUE from you actual value)

DateTime? value = Convert.IsDBNull([DATA_TABLE_VALUE]) ? null : (DateTime?)[DATA_TABLE_VALUE];



2nd option,
Since DateTime is a value type (like int), a Null value cannot be assigned to it. In this instance you can use DateTime.MinValue or

DateTime.MaxValue

instead.


If(Field[""].ToString()!="")Value=Conver.ToDateTime(Field[""])


Hi,

Set parameter property

boolIsNullable=true

when creating new sqlparameter.


这篇关于如何从数据表中检查空白值并插入存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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