I am having problems with a sql query from C# to an Sql Server, not sure what is causing it….
The code looks like this:
SqlCommand cmd = new SqlCommand(
"SELECT tscoc_Name,
tscoc_Start,
tscoc_End
FROM
tbl_SchedulerOnCall
WHERE (
tscoc_Start > @fd
AND
tscoc_End < @ld)
AND
tscoc_Start = @state",
con);
cmd.Parameters.AddWithValue("fd", SqlDbType.DateTime).Value =
startDate.ToString("yyyy-MM-dd");
cmd.Parameters.AddWithValue("ld", SqlDbType.DateTime).Value =
startDate.AddDays(14).ToString("yyyy-MM-dd");
The SQL query is this:
exec sp_executesql
N'SELECT tscoc_Name,
tscoc_Start,
tscoc_End
FROM
tbl_SchedulerOnCall
WHERE (
tscoc_Start > @fd
AND
tscoc_End < @ld)
AND
tscoc_Start = @state',
N'@fd datetime, @ld datetime, @state nvarchar(2)',
@fd = 'Oct 8 2012 12:00:00:000AM',
@ld = 'Oct 22 2012 12:00:00:000AM',
@state = N'SA'
And the error is:
Syntax error converting datetime from character string.
Anyone have any idea what is happening?
You’re converting your
DateTimevalues (assumingstartDateis indeed of typeDateTimein your .NET code) to string when assigning to theSqlParameter– what for? That’s totally useless and pointless… and it causes potential issues with date/time and language and regional settings – as you’re seeing…Just use:
and then you should be fine.