Insert date and time in European format into SQL Server from C#?

Total Post:19

Points:133
 3078  View(s)
Ratings:
Rate this:
Hi Developers!

Here's my query:

string sql = @"INSERT INTO MY_TABLE(DATE) 
                            VALUES (convert(datetime, '{0}')";
 
sql = string.Format(sql, myDate);

myDate is a C# DateTime object and has this value before the string.format: 

enter image description here

MY_TABLE has a DATE column as a DateTime type.

I got this error:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

I need to store the myDatevalue in a format like: 31/12/2013 23:59:00 into MY_TABLE.

thanks in Advance

  1. Post:105

    Points:735
    Re: Insert date and time in European format into SQL Server from C#?

    Hello!


    better to use sql parameters

    string sql = @"INSERT INTO MY_TABLE(DATE) 
    
                                VALUES (@DATE)";

    add the parameter to your SqlCommand

    command.Parameters.AddWithValue("@DATE", myDate);

      Modified On Mar-30-2018 05:08:59 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!