如何使用 C# 检查数据表行中的每个值
我有两个名为 visits
和 members
的表,我通过使用以下查询获取数据。
string sql= @"SELECT member_Firstname, member_Lastname, member_Postcode,
visit_DateTime, visit_Status, visit_Logout_DateTime, visits.member_Id, visit_AlertMsg
FROM members, visits
WHERE members.member_Id = visits.member_Id
AND members.member_Active LIKE 'y%'";
在这里,我通过与组合框值进行一些比较来获取访问日期时间值
datatable dt = helper.getdata(sql)
foreach (DataRow row in dt.Rows)
{
if (row["visit_Logout_DateTime"] != null)
{
DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
if (dtlogout != null)
{
if (cbPeriod.Text == "Today")
{
newItem.lblTime.Text = dtlogout.ToString("HH':'mm':'ss");
}
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
}
}
}
但我在这一行收到错误 DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
error : 字符串未被识别为有效的日期时间(因为该行中有一个值为空)
行“visit_Logout_DateTime”
我已经获得了“visit_Logout_DateTime”<的值/code> 像这样....
firstname lastname postcode visit_Logout_DateTime
------------- -------- --------- ---------------------
rob peter hhd344h
peter chan hy78kjk 2011-09-08 12:09:08
rock sam yudufg3746h 2011-08-08 09:08:45
我已经尝试过检查此visit_Logout_DateTime的空值,就像我上面提到的那样..
但是我未能检查该行中的任何值是否为空...
如何检查每个这一行(行[“visit_Logout_DateTime”])中的值是否为空,
请有人帮助我解决这些问题......
非常感谢......
I have two tables called visits
and members
and i am getting the data by using following query..
string sql= @"SELECT member_Firstname, member_Lastname, member_Postcode,
visit_DateTime, visit_Status, visit_Logout_DateTime, visits.member_Id, visit_AlertMsg
FROM members, visits
WHERE members.member_Id = visits.member_Id
AND members.member_Active LIKE 'y%'";
at here i am getting the visit_DateTime values with by using some comparisons with combobox values
datatable dt = helper.getdata(sql)
foreach (DataRow row in dt.Rows)
{
if (row["visit_Logout_DateTime"] != null)
{
DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
if (dtlogout != null)
{
if (cbPeriod.Text == "Today")
{
newItem.lblTime.Text = dtlogout.ToString("HH':'mm':'ss");
}
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
}
}
}
but i got the error at this line DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
error : string was not recognised as valid datetime (because of one value in that row is empty)
the row "visit_Logout_DateTime"
i have got the values of "visit_Logout_DateTime"
like this....
firstname lastname postcode visit_Logout_DateTime
------------- -------- --------- ---------------------
rob peter hhd344h
peter chan hy78kjk 2011-09-08 12:09:08
rock sam yudufg3746h 2011-08-08 09:08:45
i have tried that for checking the empty values of this visit_Logout_DateTime like i mentioned above..
but i have failed for chacking ever value is empty or not in that row...
how to check every value in this row (row["visit_Logout_DateTime"]) is empty or not
would any one pls help me on this guys ...
many thanks....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(5)
第一件事 - 当您不确定该值是否是有效对象时,使用 TryParse 而不是 Parse 来解析值。
要检查所有值,请尝试以下代码示例来检查每个值:
DataTable dt = new DataTable("MyTable");
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
if (row[column] != null)
{
string value = row[column].ToString();
if (!String.IsNullOrEmpty(value))
{
// Do something
}
}
}
}
为了扩展克里斯托夫的答案,您可以查看扩展方法来稍微整理一下代码;
public static DateTime? GetDate( this DataRow Row, string ColumnName )
{
object Value = Row[ ColumnName ];
if( Value == DBNull.Value )
return null;
else
return (DateTime)Value;
}
// Repeat for other types, or use a generic version.
...
DateTime? dtlogout = row.GetDate("visit_Logout_DateTime");
请注意 Nullable DateTime(尾随?)的使用,以便它可以接受空值。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您应该检查列的内容是否为
null
,而不是检查列是否为null
。您可以通过将其与 DBNull.Value 进行比较来做到这一点:Instead of checking if the column is
null
, you should check if the contents of the column arenull
. You can do that by comparing it toDBNull.Value
: