如何使用c#在消息框中显示数据库中的数据列表?

本文关键字:数据库 数据 列表 显示 消息 何使用 | 更新日期: 2023-09-27 17:50:19

我正在使用WinForms和c#,并且我想显示具有过期卡日期的员工列表。我试过下面的代码:

DataManager.NotificationManager obj = new DataManager.NotificationManager();
DataTable dt1 = obj.GetListExpiryDate();
DateTime currendate = DateTime.Today;
foreach (DataRow row in dt1.Rows)
{
    if (DateTime.Parse(row.ItemArray[41].ToString()) == currendate)
    {                   
        MessageBox.Show("Expired carte for the employee" + row["EmpFName"]);
    }
}

存储过程:

CREATE PROCEDURE [dbo].[GetListExpiryDate]  
AS
BEGIN
    SELECT *    
    FROM MST_Employee  
END

我需要在消息框中显示具有到期卡日期的员工列表,但问题是只显示第一个。有什么想法吗?

如何使用c#在消息框中显示数据库中的数据列表?

希望这是你想要完成的。为了获得更好的性能,请使用StringBuilder而不是string

string message = string.Empty;
foreach (DataRow row in dt1.Rows)
{
      if (DateTime.Parse(row.ItemArray[41].ToString()) == currendate)
      {
          message += "Expired carte for the employee" + row["EmpFName"] + "'n";
      }
}
 MessageBox.Show(message);

row["columnname"]代替row.ItemArray[41]

String.Builder版本:

StringBuilder sb = new StringBuilder();
 foreach (DataRow row in dt1.Rows)
{
      if (DateTime.Parse(row.ItemArray[41].ToString()) == currendate)
      {
          sb.AppendLine("Expired carte for the employee" + row["EmpFName"]); 
      }
}
MessageBox.Show(sb.ToString());

你可能也想看看这个问题的答案,如何在MessageBox中获得列表