G
Guest
I have three database queries that, as listed here, do the same thing. #1
uses two parameters, #2 uses one parameter, and #3 uses none.
1)
SqlCommand cmd = new SqlCommand("SELECT * FROM DataView WHERE Date>=@Start
AND Date<=@End", conn);
cmd.Parameters.AddWithValue("@Start", day);
cmd.Parameters.AddWithValue("@End", day);
2)
SqlCommand cmd = new SqlCommand("SELECT * FROM DataView WHERE Date>=@Start
AND Date<='"+day.ToShortDateString()+"'", conn);
cmd.Parameters.AddWithValue("@Start", day);
3)
SqlCommand cmd = new SqlCommand(
"SELECT * FROM DataView WHERE Date>='" + day.ToShortDateString() + "' AND
Date<='" + day.ToShortDateString() + "'", conn);
When I execute these, either with a SqlDataAdapter or SqlDataReader, #2 & #3
run in less than half a second but #1 consistently takes 3 seconds!!!
Running the queries directly in SQL Server Management Studio gives nearly
instantaneous returns on all three, which makes me think that the problem is
in .NET somewhere.
Can anyone offer hints as to what is bogging down in .NET or how I can work
around it to get decent performance with two parameters?
uses two parameters, #2 uses one parameter, and #3 uses none.
1)
SqlCommand cmd = new SqlCommand("SELECT * FROM DataView WHERE Date>=@Start
AND Date<=@End", conn);
cmd.Parameters.AddWithValue("@Start", day);
cmd.Parameters.AddWithValue("@End", day);
2)
SqlCommand cmd = new SqlCommand("SELECT * FROM DataView WHERE Date>=@Start
AND Date<='"+day.ToShortDateString()+"'", conn);
cmd.Parameters.AddWithValue("@Start", day);
3)
SqlCommand cmd = new SqlCommand(
"SELECT * FROM DataView WHERE Date>='" + day.ToShortDateString() + "' AND
Date<='" + day.ToShortDateString() + "'", conn);
When I execute these, either with a SqlDataAdapter or SqlDataReader, #2 & #3
run in less than half a second but #1 consistently takes 3 seconds!!!
Running the queries directly in SQL Server Management Studio gives nearly
instantaneous returns on all three, which makes me think that the problem is
in .NET somewhere.
Can anyone offer hints as to what is bogging down in .NET or how I can work
around it to get decent performance with two parameters?