如何使用 C# 检查数据表行中的每个值

发布于 12-13 01:48 字数 1840 浏览 1 评论 0原文

我有两个名为 visitsmembers 的表,我通过使用以下查询获取数据。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱,才寂寞2024-12-20 01:48:01

您应该检查列的内容是否为 null,而不是检查列是否为 null。您可以通过将其与 DBNull.Value 进行比较来做到这一点:

if (row["visit_Logout_DateTime"] != DBNull.Value)
{
    ...
}

Instead of checking if the column is null, you should check if the contents of the column are null. You can do that by comparing it to DBNull.Value:

if (row["visit_Logout_DateTime"] != DBNull.Value)
{
    ...
}
若有似无的小暗淡2024-12-20 01:48:01

第一件事 - 当您不确定该值是否是有效对象时,使用 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
                    }
                }
            }
        }

First thing - use TryParse, not Parse to parse values when you're not sure whether the value will be a valid object or not.

For checking all values, try the following code example for checking each value:

        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
                    }
                }
            }
        }
猥琐帝2024-12-20 01:48:01

您可以像这样使用 DateTime.TryParse

DateTime date = new DateTime();

if (DateTime.TryParse(row["visit_Logout_DateTime"], out date))
            dtlogout = date;

You can use DateTime.TryParse like this:

DateTime date = new DateTime();

if (DateTime.TryParse(row["visit_Logout_DateTime"], out date))
            dtlogout = date;
帅气称霸2024-12-20 01:48:01

您无法检查 null 行。您应该与DBNull.Value进行比较。
所以,要么选择:

row["visit_Logout_DateTime"] != DBNull.Value

要么

!string.IsNullOrEmpty(row["visit_Logout_DateTime"].ToString())

You cannot check row against null. You should compare to DBNull.Value.
So, either go with:

row["visit_Logout_DateTime"] != DBNull.Value

or

!string.IsNullOrEmpty(row["visit_Logout_DateTime"].ToString())
优雅的叶子2024-12-20 01:48:01

为了扩展克里斯托夫的答案,您可以查看扩展方法来稍微整理一下代码;

 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(尾随?)的使用,以便它可以接受空值。

To expand on Kristof's answer you could look at extension methods to tidy the code up a little;

 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");

Note the use of the Nullable DateTime (trailing ?) so that it can accept nulls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文