我正在将日期写到MYSQL表中,当我使用MYSQL Workbench查询表时,日期显示为“ yyyy-MM-dd hh:mm:ss”。执行读取器时,表中日期的格式返回为“ dd / MM / yyyy hh:mm:ss”。这是为什么?为什么在我存储它时不将其退回。它想让我以该格式存储它,为什么它不以该格式返回它?
存储:2018-11-20 09:32:23 返回:11/20/2018 9:32:23
var mySqlQuery = "SELECT * FROM purchase_order WHERE purchase_order_number LIKE '" + cmbPurchaseOrderNumbers.Text + "'";
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
using (var command = new MySqlCommand(mySqlQuery, connection))
{
using (var reader = command.ExecuteReader())
{
//Iterate through the rows and add it to the combobox's items
while (reader.Read())
{
lblPoNumber.Text = reader.GetString("purchase_order_number");
cmbBillTo.Text = reader.GetString("purchase_order_bill_to");
cmbShipTo.Text = reader.GetString("purchase_order_ship_to");
cmbWareHouse.Text = reader.GetString("purchase_order_location");
cmbVendors.Text = reader.GetString("purchase_order_vendor");
txtPoDate.Text = (reader.GetString("purchase_order_date")).Substring(0, (reader.GetString("purchase_order_date").Length) - 2).Trim();
}
}
}
}
答案 0 :(得分:0)
var mySqlQuery = "SELECT * FROM purchase_order WHERE purchase_order_number LIKE '" + cmbPurchaseOrderNumbers.Text + "'";
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
using (var command = new MySqlCommand(mySqlQuery, connection))
{
using (var reader = command.ExecuteReader())
{
//Iterate through the rows and add it to the combobox's items
while (reader.Read())
{
lblPoNumber.Text = reader.GetString("purchase_order_number");
cmbBillTo.Text = reader.GetString("purchase_order_bill_to");
cmbShipTo.Text = reader.GetString("purchase_order_ship_to");
cmbWareHouse.Text = reader.GetString("purchase_order_location");
cmbVendors.Text = reader.GetString("purchase_order_vendor");
txtPoDate.Text = reader.GetDateTime().ToString("yyyy-MM-dd hh:mm:ss");
}
}
}
}
将日期作为DateTime
对象而不是字符串。然后,您可以根据需要设置其格式。