Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将T-SQL中的空datetime值返回到C#DataTable==>;DataRow而不是空字符串_C#_.net_Tsql_Datetime_String - Fatal编程技术网

将T-SQL中的空datetime值返回到C#DataTable==>;DataRow而不是空字符串

将T-SQL中的空datetime值返回到C#DataTable==>;DataRow而不是空字符串,c#,.net,tsql,datetime,string,C#,.net,Tsql,Datetime,String,此值(行[10])来自T-SQL中的一个SQL结果集的DataRow对象。我相信它有一个类型“object”。在我的数据库中,对于这个特定记录,这个字段的值为NULL,但它在结果集中返回一个空字符串,而不是NULL值。我想修复根问题,让结果集返回NULL而不是空字符串,但如果不可能,那么让这段代码更高效就好了——这意味着第一段代码,因为它适用于所有3种情况 当第[10]行.ToString()等于空字符串、null或日期时间格式时,此操作有效,但我想缩短它。这是我目前的解决办法。 这适用于空da

此值(行[10])来自T-SQL中的一个SQL结果集的DataRow对象。我相信它有一个类型“object”。在我的数据库中,对于这个特定记录,这个字段的值为NULL,但它在结果集中返回一个空字符串,而不是NULL值。我想修复根问题,让结果集返回NULL而不是空字符串,但如果不可能,那么让这段代码更高效就好了——这意味着第一段代码,因为它适用于所有3种情况

当第[10]行.ToString()等于空字符串、null或日期时间格式时,此操作有效,但我想缩短它。这是我目前的解决办法。

这适用于空datetime值,即实际的datetime值,但不适用于第[10]行等于空字符串的情况。


对所述问题的回答

string temp0 = row[10].ToString();
DateTime?;
if (string.IsNullOrEmpty(temp0)) 
{
    temp = null;
}
else
{
    temp = DateTime.Parse(temp0);
}
可以使用DateTimerParse。

可以传递空值,但它将解析为1/1/0001

所述问题的答案

string temp0 = row[10].ToString();
DateTime?;
if (string.IsNullOrEmpty(temp0)) 
{
    temp = null;
}
else
{
    temp = DateTime.Parse(temp0);
}
可以使用DateTimerParse。

可以传递null,但它将解析为1/1/0001

以下内容应作为快捷方式:

DateTime? temp = String.IsNullOrEmpty(row[10].ToString())? null : DateTime.Parse(temp0);

以下应作为快捷方式使用:

DateTime? temp = String.IsNullOrEmpty(row[10].ToString())? null : DateTime.Parse(temp0);
尝试:

尝试:


正确的方法是:

 DateTime? temp = null; //this is fine
 var indexOfYourColumn = reader.GetOrdinal("columnName");//not necessary but makes iterating faster
 while (reader.Read())
 {
       temp = reader[indexOfYourColumn] != DBNull.Value ? (DateTime?) null : DateTime.Parse(reader[indexOfYourColumn].ToString())
 }
为什么??不能对null执行.ToString()。然后,你需要按喜欢的方式来投球。所以从可空DateTime到可空DateTime


这是很久以前提出的问题。但是公认的答案是不正确的。

正确的方法是:

 DateTime? temp = null; //this is fine
 var indexOfYourColumn = reader.GetOrdinal("columnName");//not necessary but makes iterating faster
 while (reader.Read())
 {
       temp = reader[indexOfYourColumn] != DBNull.Value ? (DateTime?) null : DateTime.Parse(reader[indexOfYourColumn].ToString())
 }
为什么??不能对null执行.ToString()。然后,你需要按喜欢的方式来投球。所以从可空DateTime到可空DateTime


这是很久以前提出的问题。但接受的答案不正确。

尝试检查
DBNull
。我想您知道,
DBNull
表示字段中的空值,而
null
表示c#中的引用类型变量,它没有赋值。
行[10]
是否引用了
varchar
类型的字段?@shahkalpesh。不这是一个datetimeTry检查
DBNull
。我想您知道,
DBNull
表示字段中的空值,而
null
表示c#中的引用类型变量,它没有赋值。
行[10]
是否引用了
varchar
类型的字段?@shahkalpesh。不这是一个约会时间,我必须在它开始工作之前播放最后一段声明。d、 date\u migrate\u prod=String.IsNullOrEmpty(行[10].ToString())?null:(DateTime?)DateTime.Parse(行[10].ToString())。。。。。。我还没有弄清楚如何让System.DBNull工作到现在(在我的评论中),这是可行的,所以我将其标记为答案。如果有人想帮助解决问题的根本原因,我会测试你的答案。我必须在它起作用之前抛出声明的最后一部分。d、 date\u migrate\u prod=String.IsNullOrEmpty(行[10].ToString())?null:(DateTime?)DateTime.Parse(行[10].ToString())。。。。。。我还没有弄清楚如何让System.DBNull工作到现在(在我的评论中),这是可行的,所以我将其标记为答案。如果有人想帮助解决根本原因,我将测试您的答案。@010001100110000101110011010您的答案:Try localvariable=row[10]==Convert.DBNull?null:行[10]。ToString()。。。。。。。获取错误:无法将类型“string”隐式转换为“System.DateTime?”,错误:DateTime?localvariable=行[10]==Convert.DBNull?null:Convert.ToDateTime(第[10]行).ToString())@0100011001100001110010011010回答:Try localvariable=row[10]==Convert.DBNull?null:行[10]。ToString()。。。。。。。获取错误:无法将类型“string”隐式转换为“System.DateTime?”,错误:DateTime?localvariable=行[10]==Convert.DBNull?null:Convert.ToDateTime(行[10].ToString())