how to insert datetime into the SQL Database table?

前端 未结 4 1499
天涯浪人
天涯浪人 2020-12-05 01:15

How can I insert datetime into the SQL Database table ? Is there a way to insert this query through the insert command in C# / .NET?

4条回答
  •  抹茶落季
    2020-12-05 02:08

    You will need to have a datetime column in a table. Then you can do an insert like the following to insert the current date:

    INSERT INTO MyTable (MyDate) Values (GetDate())
    

    If it is not today's date then you should be able to use a string and specify the date format:

    INSERT INTO MyTable (MyDate) Values (Convert(DateTime,'19820626',112)) --6/26/1982
    

    You do not always need to convert the string either, often you can just do something like:

    INSERT INTO MyTable (MyDate) Values ('06/26/1982') 
    

    And SQL Server will figure it out for you.

提交回复
热议问题