SqlCommand . ExecuteReader method slow

64 Views Asked by At

I have a performance problem with SqlDataReader object.

I am used to use Entity Framework but for a specific reason, I should use ADO.NET queries in my app.

The problem is, when I execute my query, it takes almost 10 seconds.

I run exactly the same query in SQL Server Management Studio, it takes only 1 second.

I thought that maybe it's the SqlDataReader and I tried to fill a DataTable with a SqlDataAdapter, but I get the same result.

What is wrong with my code? Is there any way to improve the performance of SqlDataReader?

Here is the query and C# code.

string commandText = "SELECT Pati_FirstName, Pati_LastName, Pati_Gender AS Gender, "
        + " Pati_HeadQuarterID, Pati_AgencyID, Pati_Address1, Pati_Address2, Pati_BirthDate, Pati_FullName, Pati_FirstNameIfDiff, Pati_LastNameIfDiff, Pati_FootSize, Pati_ID, Pati_Height, Pati_MailAddress,"
        + " Pati_MobileNumber, Pati_PhoneNumber, Pati_PostCode, Pati_SecurityNumber, Pati_Weight,"
        + " Agen_Name, Head_Name, pat.Path_Name AS Pati_Pathologie,"
        + " gender.Capt_CulturInfo AS Pati_Gender, prof.Capt_CulturInfo AS Pati_Profession, title.Capt_CulturInfo AS Pati_Title, sport1.Capt_CulturInfo AS Pati_Sport1, sport2.Capt_CulturInfo AS Pati_Sport2,"
        + " p1.Pres_FullName AS Prescriber1, p2.Pres_FullName AS Prescriber2, pSender.Pres_FullName AS SentBy, Prac_FullName, City_Name"

        + " FROM Patient INNER JOIN HeadQuarter ON Head_ID = Pati_HeadQuarterID INNER JOIN"
        + " Agency ON Agen_ID = Pati_AgencyID LEFT OUTER JOIN"
        + " CustomCaption AS gender ON gender.Capt_Family = 'Pati_Gender' AND gender.Capt_Code = Pati_Gender LEFT OUTER JOIN"
        + " CustomCaption AS prof ON prof.Capt_Family = 'profession' AND prof.Capt_Code = Pati_Profession LEFT OUTER JOIN"
        + " Pathology AS pat ON pat.Path_ID = Pati_Pathologie LEFT OUTER JOIN"
        + " CustomCaption AS title ON title.Capt_Family = 'title' AND title.Capt_Code = Pati_Title LEFT OUTER JOIN"
        + " CustomCaption AS sport1 ON sport1.Capt_Family = 'sports' AND sport1.Capt_Code = Pati_Sport1 LEFT OUTER JOIN"
        + " CustomCaption AS sport2 ON sport2.Capt_Family = 'sports' AND sport2.Capt_Code = Pati_Sport2 LEFT OUTER JOIN"
        + " Prescriber AS p1 ON p1.Pres_ID = Pati_Doctor1 LEFT OUTER JOIN"
        + " Prescriber AS p2 ON p2.Pres_ID = Pati_Doctor2 LEFT OUTER JOIN"
        + " Prescriber AS pSender ON pSender.Pres_ID = Pati_SentBy LEFT OUTER JOIN"
        + " Practitioner ON Prac_ID = Pati_PracticionerID LEFT OUTER JOIN"
        + " City ON City_ID = Pati_CityID"
        + " WHERE Pati_Deleted IS NULL AND Pati_AgencyID = @AgencyID ORDER BY Pati_LastName, Pati_FirstName"; 

List<VPatient> retVal = new List<DataRepository.VPatient>();

try
{
    using (SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
    {
        using (SqlCommand sqlComm = new SqlCommand("", sqlConn))
        {
            sqlComm.CommandText = commandText.Replace("_CulturInfo", "_" + currentCultur);
            sqlComm.Parameters.AddWithValue("@AgencyID", agencyID);

            sqlComm.Connection.Open();

            // This line takes 10 seconds. 
            SqlDataReader sqlRd = sqlComm.ExecuteReader();

            // I tried this as an alternative, it doesn't change anything.
            // SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlComm);
            // System.Data.DataTable dtPatients = new System.Data.DataTable();
            // sqlAdp.Fill(dtPatients);

            VPatient pat;

            while (sqlRd.Read())
            {
                pat = new VPatient();
                pat.Pati_HeadQuarterID = sqlRd["Pati_HeadQuarterID"].ToString();
                pat.Pati_AgencyID = sqlRd["Pati_AgencyID"].ToString();
                ....

                retVal.Add(pat);
            }
        }
    }
}
catch (Exception ex)
{
    CoreMethods.ParseError(ex, "GetPatientList");
}

And if I execute the same query in SQL Server Management Studio, it takes only 1 second.

0

There are 0 best solutions below