简体   繁体   English

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

[英]How to Retrieve datetimepicker value from mysql back to datetimepicker of my c#?

So i have a datetimepicker inside my Windows Form Application (C#)所以我的 Windows 窗体应用程序 (C#) 中有一个日期时间选择器

(variable name = birthdatepPicker) CustomFormat : "MM - dd - yyy". (变量名称 =birthdatepPicker)自定义格式:“MM - dd - yyy”。

I successfully insert it in MySql Database using (this.birthdatePicker.Text)我使用 (this.birthdatePicker.Text) 成功将它插入到 MySql 数据库中

Column Name = birthday and DataType = (VARCHAR(45)).列名 = 生日和数据类型 = (VARCHAR(45))。

actually .. the one that retrieves the values from my database is a combobox which is inside my groupbox..实际上..从我的数据库中检索值的是一个组合框,它位于我的分组框内。

My Question.我的问题。 How can I retrieve the data of my datetimepicker that is saved from MySql back to my datetimepiker.如何将从 MySql 中保存的 datetimepicker 的数据检索回我的 datetimepiker。

          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;

If you don't change the data type on the birthday column in your database, you'll have to parse the string back into a valid DateTime using code like the following:如果不更改数据库中生日列的数据类型,则必须使用如下代码将字符串解析回有效的 DateTime:

DateTime birthday;

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

Since you were able to change the type to a DateTime, you should just be able to read it back out:由于您能够将类型更改为 DateTime,因此您应该能够将其读回:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM