DateTime format to SQL format using C#

前端 未结 15 1202
伪装坚强ぢ
伪装坚强ぢ 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: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

提交回复
热议问题