I have a web service, so the handler is called multiple times concurrently all the time.
Inside I create SqlConnection and SqlCommand. I have to execute about 7 different commands. Different commands require various parameters, so I just add them once:
command.Parameters.Add(new SqlParameter("@UserID", userID));
command.Parameters.Add(new SqlParameter("@AppID", appID));
command.Parameters.Add(new SqlParameter("@SID", SIDInt));
command.Parameters.Add(new SqlParameter("@Day", timestamp.Date));
command.Parameters.Add(new SqlParameter("@TS", timestamp));
Then during execution I just change CommandText prorerty and then call ExecuteNonQuery(); or ExecuteScalar();
And I face performance issue. For example little debuggin and profiling shows, that command
command.CommandText = "SELECT LastShowTS FROM LogForAllTime WHERE UserID = @UserID";
takes about 50ms in avarage. If I change it to:
command.CommandText = "SELECT LastShowTS FROM LogForAllTime WHERE UserID = '" + userID.Replace("\'", "") + "'";
then it takes only 1ms in avarage!
I just can't get a clue where to investigate the problem.
userID
in the C#, and what is the defined type of theUserID
column in the database? – Lilalilacstring
is unicode, so (by default) maps to nvarchar, not varchar. – Lilalilac