从mysql检索的日期数据不是c#标签上显示的确切数据

时间:2019-02-06 05:55:26

标签: c# mysql

我想在label上显示特定列的内容,但是当我到达date数据类型中的特定列时。它添加了“ 12:00:00” AM”,我想将其删除,以便仅将正确的日期和时间对以字符串形式连接在一起。我不允许更改数据库表。 我怎么能消除字符串的“ 12:00:00 AM”部分呢?有可能吗? 我确定它是date数据类型,但它给出的数据看起来像datetime数据类型。 好吧,我显然是新手,任何建议或帮助都将受到高度赞赏。非常感谢。

the part of the database

when i retrieve a certain data in the db table, the label is not showing the exact same thing thats on the database

private void dbdateBtn_Click(object sender, EventArgs e)
    {
        //db read
        string constring = "SERVER = localhost; user id = root; password =; database = mpdb";
        string Query = "select * from mpdb.cicotbl where cico_no='" + this.textBox1.Text + "';";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string firstdate = myReader.GetString("CINd");
                string seconddate = myReader.GetString("COUTd");
                string time1 = myReader.GetString("CINt");
                string time2 = myReader.GetString("COUTt");
                dbdate1.Text = firstdate + " " + time1;
                dbdate2.Text = seconddate + " " + time2;
            }
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
    }

1 个答案:

答案 0 :(得分:1)

我相信是因为日期以日期时间变量的形式存储在数据库中,因此可以使用以下简短解决方案:

DateTime firstdate = DateTime.Parse(myReader.GetString("CINd"));
DateTime seconddate = DateTime.Parse(myReader.GetString("COUTd"));
string firstdatestring = firstdate.ToLongDateString();
string seconddatestring = seconddate.ToLongDateString();
string time1 = myReader.GetString("CINt");
string time2 = myReader.GetString("COUTt");
dbdate1.Text = firstdatestring + " " + time1;
dbdate2.Text = seconddatestring + " " + time2;