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