我正在尝试将 SQL Server 中的日期记录与系统日期进行比较。在我的示例中,用户首先注册他的姓名和出生日期,然后将其存储在数据库中。然后,用户只使用他的名字登录 Web 应用程序。登录后,他的名字会显示在"Welcome "player name“使用Sessions.
除了他的名字之外,我还想显示一条消息,如果他的出生日期与系统日期匹配,则显示“生日快乐”。我尝试过使用System.DateTime.Now,但我认为它也在比较年份,而我真正想要的只是日期和月份。我真的很感激任何建议或帮助。
登录页面中的代码:
protected void Button1_Click(object sender, EventArgs e)
{
  String name = TextBox1.Text;
  String date = System.DateTime.Today.ToShortDateString();
  SqlConnection myconn2 = new 
    SqlConnection(ConfigurationManager.ConnectionStrings["User"].ToString()); 
  SqlCommand cmd2 = new SqlCommand();
  SqlDataReader reader;
  myconn2.Open();
  cmd2 = new SqlCommand("Select D_O_B from User WHERE Username = @username", 
                        myconn2);
  cmd2.Parameters.Add("@username", SqlDbType.NVarChar).Value = name;
  cmd2.Connection = myconn2                           
  cmd2.ExecuteNonQuery();
  reader = cmd2.ExecuteReader();
  while (reader.Read().ToString() == date)
  {
    Session["Birthday"] = "Happy Birthday";
  }
}
注意:我在上面的代码中使用了相同的阅读器,但这里的阅读器使用不同的连接。另外,reader.Read()不同于reader.HasRows?
Web 应用程序页面中的代码:
string date = (string)(Session["Birthday"]); // Retrieving the session
Label6.Text = date;