[英]how to check for null in datarow row in c#
I have this code 我有这个代码
foreach (DataRow row in DTgraph.Rows)
{
String UserName = row["UserName"].ToString();
String LoggedState = row["LoggedState"].ToString();
String InteractionId = row["InteractionId"].ToString();
String InteractionType = row["InteractionType"].ToString();
}
how to check if the row["something"]
is null? 如何检查
row["something"]
是否为null?
I tried to run the code, and the null values becomes "" (empty). 我试图运行代码,并且null值变为“”(空)。
I need to check if these is null. 我需要检查这些是否为空。
I now that this is an stupid question, but my problem is that I am making ToString()
so i thought that null becomes null
or NULL
or Null
or empty? 我现在这是一个愚蠢的问题,但是我的问题是我正在制作
ToString()
所以我认为null变为null
或NULL
或Null
或为空?
thanks 谢谢
use DBNull.Value 使用DBNull.Value
var UserName = row["UserName"].ToString();
to 至
var UserName = reader["UserName"] != DBNull.Value ? row["UserName"].ToString():"";
Update 更新
var UserName = "";
if(reader["UserName"] != DBNull.Value)
{
UserName = row["UserName"].ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.