如何检索datetimepicker值从mysql回到我的c#的datetimepicker

本文关键字:datetimepicker 我的 mysql 值从 检索 何检索 | 更新日期: 2023-09-27 18:17:25

所以我有一个日期时间选择器在我的Windows窗体应用程序(c#)

(variable name = birthdatepPicker) CustomFormat: "MM - dd - yyy"。

我成功插入MySql数据库使用(this.birthdatePicker.Text)

Column Name = birthday and DataType = (VARCHAR(45))。

其实. .从我的数据库中检索值的是一个组合框,它在我的组框内。

我的问题。我如何检索我的datetimepicker的数据是从MySql保存回我的datetimepicker。

          string connect = "server=localhost;port=3307;username=root;password=keepthepromise";
          string query = "select * from database.employeeinfo where first_name='"+ select_update_combox.Text+"' ;";
          MySqlConnection connecttodtb = new MySqlConnection(connect);
          MySqlCommand querycmd = new MySqlCommand(query, connecttodtb);
          MySqlDataReader reader;
          try
          {
              connecttodtb.Open();
              reader = querycmd.ExecuteReader();
              while (reader.Read())
              {
                  string empID = reader.GetString("employeeID");
                  string user = reader.GetString("username");
                  string password = reader.GetString("password");
                  string conpass = reader.GetString("password");
                  string nameNdFN = reader.GetString("first_name");
                  string middlename = reader.GetString("middle_name");
                  string lastname = reader.GetString("last_name");
                  string birthday = reader.GetString("birthday");
                  string gender1 = reader.GetString("gender");
                  string email = reader.GetString("email");
                  string address1 = reader.GetString("address");

                  //------------------------------------------------
                  regEmpID_text.Text = empID;
                  reguser_txt.Text = user;
                  regpass_txt.Text = password;
                  regconpass_txt.Text = password;
                  regname_txt.Text = nameNdFN;
                  regmiddlename_txt.Text = middlename;
                  reglastname_txt.Text = lastname;

                  regaddress_txt.Text = address1;
                  regemail_txt.Text = email;

如何检索datetimepicker值从mysql回到我的c#的datetimepicker

如果不更改数据库中生日列的数据类型,则必须使用如下代码将字符串解析回有效的DateTime:

DateTime birthday;
if (DateTime.TryParseExact(reader.GetString("birthday"), "MM dd yyyy",
    CultureInfo.CurrentCulture, DateTimeStyles.None, out birthday))
{
    birthdatepPicker.Value = birthday;
}

既然您能够将类型更改为DateTime,那么您应该能够将其读取出来:

birthdatepPicker.Value = reader.GetDateTime("birthday");