0

我对对象有性能问题SqlDataReader

我习惯使用实体框架,但出于特定原因,我应该在我的应用程序中使用 ADO.NET 查询。

问题是,当我执行查询时,几乎需要 10 秒。

我在 SQL Server Management Studio 中运行完全相同的查询,只需要 1 秒。

我想也许是SqlDataReader并且我试图用 a 填充 a DataTableSqlDataAdapter但我得到了相同的结果。

我的代码有什么问题?有什么方法可以提高性能SqlDataReader吗?

这是查询和 C# 代码。

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");
}

如果我在 SQL Server Management Studio 中执行相同的查询,只需 1 秒。

4

0 回答 0