为什么date.ToString()会随时间显示

3 c# sql sql-server winforms

我的datesql-server数据库中有一个数据类型的列,我将其插入其中1999-12-23.当我在我的数据库中运行select查询时显示日期为,1999-12-23但是当我将数据库连接到我的c#winform应用程序并检索1999-12-23 00:00:00它显示的日期时(即它显示日期和时间).

这些是我使用的代码

创建表

CREATE TABLE Users.Personal
(
/*...Other Codes for the table this is the main problem*/
DateofReg date NOT NULL
)
Run Code Online (Sandbox Code Playgroud)

选择查询

SELECT * FROM Users.Personal
Run Code Online (Sandbox Code Playgroud)

(显示日期为1999-12-23)

连接数据库

private void RetrievePersonalDetails()
{
    SqlConnection myConnection = new SqlConnection("server=AMESINLOLA;" +
        "Trusted_Connection=yes;" +
        "database=Crm_Db;");
    myConnection.Open();

    SqlCommand myCommand = myConnection.CreateCommand();
    myCommand.CommandText = "SELECT * FROM Users.Personal WHERE UniqueID='" + uniqueid + "'";
    myCommand.CommandType = CommandType.Text;

    SqlDataReader myReader = myCommand.ExecuteReader();

    if (myReader.Read())
    {
        //Other codes inserting to textbox but this is the main problem
        txtDor.Text = myReader["DateofReg"].ToString();
    }
    else
    {
        MessageBox.Show("Empty");
    }
    myConnection.Close();
    myReader.Close();
}
Run Code Online (Sandbox Code Playgroud)

(显示日期为1999-12-23 00:00:00)

为什么日期在应用程序中显示时间但在数据库中显示良好,我该怎么做只显示日期?

Mic*_*haC 6

myReader["DateofRef"]似乎返回了一个DateTime对象.这内部存储日期值的刻度(包括时间和毫秒等).

ToString 为DateTime对象应用默认格式.

你也可以使用 DateTime.Now.ToShortDateString()哪些只打印年,月和日.

格式虽然取决于当前的culture(Thread.CurrentThread.CurrentCulture),ToStrin也需要一个名为的参数IFormatProvider,这可以设置为你想要指定日期字符串应该如何使用的任何文化CultureInfo...

您可以通过将格式传递给ToString方法来更改格式.

很多例子可以在这里找到http://msdn.microsoft.com/en-US/library/zdtaw1bw(v=vs.110).aspx