How to save Local time in Mysql database C#

时间:2017-10-12 09:52:14

标签: c# mysql time

I am trying to make a system that logs reports of who comes into the system , the username and the Time the person logged into the system, now i get this 0000-00-00 00:00:00 as the time and i want it to always get something like this : 2017-10-12 05:30:35, My code looks like this :

  public void LodgeReport()
    {
        string connectionstring = ConfigurationManager.ConnectionStrings["MySQLDatabaseConnection"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(connectionstring))
        {
            DateTime localDate = DateTime.Now;

            //string time = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
            string time = localDate.ToString("MM'/'dd'/'yyyy HH':'mm':'ss.fff");
            string sql2 = "insert into vms_db.login_reports (username,time) values (@username,@time)";
            using (MySqlCommand cmd2 = new MySqlCommand(sql2, con))
            {
                try
                {
                    con.Open();
                    cmd2.Parameters.AddWithValue("@username", userName.Text);
                    cmd2.Parameters.AddWithValue("@time", time.ToString());
                    cmd2.ExecuteNonQuery();
                    con.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

    }

What am i Missing?

0 个答案:

没有答案