DateTime format to SQL format using C#

前端 未结 15 1005
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 02:47

I am trying to save the current date time format from C# and convert it to an SQL Server date format like so yyyy-MM-dd HH:mm:ss so I can use it for my UP

相关标签:
15条回答
  • 2020-11-28 03:08

    Your problem is in the Date property that truncates DateTime to date only. You could put the conversion like this:

    DateTime myDateTime = DateTime.Now;
    
    string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
    
    0 讨论(0)
  • 2020-11-28 03:10

    try this below

    DateTime myDateTime = DateTime.Now;
    string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
    
    0 讨论(0)
  • 2020-11-28 03:10

    The Answer i was looking for was:

    DateTime myDateTime = DateTime.Now;
    string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
    

    I've also learned that you can do it this way:

    DateTime myDateTime = DateTime.Now;
    string sqlFormattedDate = myDateTime.ToString(myCountryDateFormat);
    

    where myCountryDateFormat can be changed to meet change depending on requirement.

    Please note that the tagged "This question may already have an answer here:" has not actually answered the question because as you can see it used a ".Date" instead of omitting it. It's quite confusing for new programmers of .NET

    0 讨论(0)
  • 2020-11-28 03:12

    Another solution to pass DateTime from C# to SQL Server, irrespective of SQL Server language settings

    supposedly that your Regional Settings show date as dd.MM.yyyy (German standard '104') then

    DateTime myDateTime = DateTime.Now;
    string sqlServerDate = "CONVERT(date,'"+myDateTime+"',104)"; 
    

    passes the C# datetime variable to SQL Server Date type variable, considering the mapping as per "104" rules . Sql Server date gets yyyy-MM-dd

    If your Regional Settings display DateTime differently, then use the appropriate matching from the SQL Server CONVERT Table

    see more about Rules: https://www.techonthenet.com/sql_server/functions/convert.php

    0 讨论(0)
  • 2020-11-28 03:14

    The correct answer was already given "use parameters". Formatting a date and passing it as a string to SQL-Server can lead to errors as it depends on the settings how the date is interpreted on the server side. In europe, we write '1.12.2012' to indicate december 1st 2012, whereas in other countries this might be treated as january 12th.

    When issuing statements directly in SSMS I use the format yyyymmdd which seem to be quite general. I did not encounter any problems on the various installations I worked on so far.

    There is another seldom used format, which is a bit weird but works for all versions:

    select { d '2013-10-01' }
    

    will return the first of october 2013.

    select { ts '2013-10-01 13:45:01' }
    

    will return october 1st, 1:45:01 PM

    I strongly advice to use parameters and never format your own SQL code by pasting together homegrown formatted statement fragments. It is an entry for SQL injection and strange errors (formatting a float value is another potential issue)

    0 讨论(0)
  • 2020-11-28 03:16

    only you put "T"+DateTime.Now.ToLongTimeString()+ '2015-02-23'

    0 讨论(0)
提交回复
热议问题