Take this simple code snippit
List<NJTrackingEntity> result = new List<NJTrackingEntity>();
try
{
SqlConnection cn = new SqlConnection(DBCON());
cn.Open();
string sql = "select A.SPECIALPOPULATION as 'CODE'," +
"COALESCE(B.DISPDESC,(SELECT TOP 1 C.SITENAME from TblResidentialProvLoginsPgm C " +
"LEFT OUTER JOIN xvt_userauthprograms D on " +
"C.USERID = D.USERID AND D.PGMINDICATOR = A.SPECIALPOPULATION " +
"WHERE C.USERID = D.USERID )," +
"(SELECT TOP 1 C.ORG from TBLUSERLOGINS C " +
"LEFT OUTER JOIN xvt_userauthprograms D on " +
"C.USERID = D.USERID AND D.PGMINDICATOR = A.SPECIALPOPULATION " +
"WHERE C.USERID = D.USERID )) as 'DESCRIPTION', " +
"A.EFFECTIVEDATE,A.EXPDATE,A.MSPID " +
"from dbo.tblMemberSpecialPopulation A " +
"LEFT OUTER JOIN dbo.LOOKUPTRACKINGELEMENTS B ON " +
"A.SPECIALPOPULATION = B.Code " +
"where MEMBERID = @P1 " +
"ORDER BY EFFECTIVEDATE DESC,EXPDATE DESC";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.Add("@P1", System.Data.SqlDbType.Int, 4, "MEMBERID").Value = mid;
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
NJTrackingEntity nj = new NJTrackingEntity();
nj.ID = r[4] + "";
nj.CODE = r[0] + "";
nj.DESCRIPTION = r[1] + "";
if (!r.IsDBNull(2))
nj.EFFECTIVE = r.GetDateTime(2).ToString("yyyy/MM/dd");
if (!r.IsDBNull(3))
nj.EXPIRATION = r.GetDateTime(3).ToString("yyyy/MM/dd");
result.Add(nj);
}
r.Close();
cmd.Dispose();
cn.Close();
cn.Dispose();
}
catch (Exception ex)
{
NJTrackingEntity nj = new NJTrackingEntity();
nj.DESCRIPTION = ex.Message;
result.Add(nj);
}
return result;
For some sets of memberIDs handed to this I get database timeout errors and for others it works just fine.
Pulling my hair out looking for database tuning bits and what not to make the timeouts go away. I though the only thing
I could do was to increase the timeout values on the command object. No matteer what I put there though the timeouts persisted
of course taking longer and longer to return to me the error result.
The Query however take out of code and plaaced into Query tools in SQLDev Studio executed quickly and without error.
Again this only occured with some MemberID values.
What I found was very surprising change the code to also include a simple addition to the where clause of 'and 1=1' make the issue
disappear.
So the execution engine for sql commands in .Net was creating some sort of issue that it should not have been doing?
Makes me wonder where else in the code this kind of crap might be happening....