I am trying to execute a SQL statement that grabs data within a given time period represented by two dates. Although I have verified that the values of the parameters I am swapping the SQL parameters with are the correct values, I am still getting this exception:
Syntax error converting character string to smalldatetime data type
Here is the C# code:
SqlCommand command = new SqlCommand("SELECT date, @Data FROM datasite WHERE date > '@Start' and date < '@End'", conn);
command.Parameters.AddWithValue("@Data", dataType);
var start = dateRange.StartDate.ToShortDateString();
var end = dateRange.EndDate.ToShortDateString();
command.Parameters.AddWithValue("@Start", start);
command.Parameters.AddWithValue("@End", end);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) { //... }
DateTimeitself?command.Parameters.AddWithValue("@Start", dateRange.StartDate);'@Start'should just be@Start