C# 不必要地添加 00:00:00

C# is adding 00:00:00 unnecessarily

我的 SQL 服务器正在使用 DATE 数据类型,但我的 C# 应用程序将其读取为 'mm/dd/yyyy 00:00:00',谁能告诉我如何去掉 0? (其他一切正常)...?

var sda = new SqlDataAdapter("SELECT CustomerOrders.OrderID, Product.ProductName, OrderDetails.QuantityOrdered," +
" Customer.CustomerFirstName, Customer.CustomerLastName, CustomerOrders.OrderDate FROM CustomerOrders" +
" INNER JOIN Product ON CustomerOrders.ProductID = Product.ProductID INNER JOIN Customer on CustomerOrders.CustomerID = Customer.CustomerID" +
" INNER JOIN OrderDetails ON CustomerOrders.OrderID = OrderDetails.OrderID;", DatabaseConnection); // uses SQL query to read every piece of data in database
var dataset = new DataSet();
sda.Fill(dataset);
DatabaseConnection.Close();

DatabaseList.Items.Clear();                                           // clears all data in the list
DatabaseList.BeginUpdate();
foreach (DataRow row in dataset.Tables[0].Rows)
{
    var lvi = new ListViewItem();                                     // rebuilds the list
    lvi.Text = row["OrderID"].ToString();
    lvi.SubItems.Add(row["ProductName"].ToString());
    lvi.SubItems.Add(row["QuantityOrdered"].ToString());
    lvi.SubItems.Add(row["CustomerFirstName"].ToString());
    lvi.SubItems.Add(row["CustomerLastName"].ToString());
    lvi.SubItems.Add(row["OrderDate"].ToString());
    DatabaseList.Items.Add(lvi);
}
DatabaseList.EndUpdate();

如果您不希望 DateTime 的时间部分与 String 相同,您有多种选择:

DateTime orderDate = row.Field<DateTime>("OrderDate");
lvi.SubItems.Add(orderDate.ToShortDateString());
lvi.SubItems.Add(orderDate.ToString("d"));
lvi.SubItems.Add(orderDate.ToString("dd/MM/yyyy");

阅读:Date and time format strings