我有一个通过Entity Framework与数据库连接的应用程序。
add name="dataEntities"
connectionString="metadata=res://*/Models.FVData.csdl|res://*/Models.FVData.ssdl|res://*/Models.FVData.msl;provider=System.Data.SqlClient;provider connection string="data source= :) ;initial catalog=FVData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
我有一个数据管理器类,该类调用存储过程spFinance
。
我的问题是,每次打开PC并运行该程序,并且该程序首次尝试执行存储过程时,都会收到超时异常。
但是第二次我做同样的事情,只需要10秒就可以执行相同的数据。
public class DataLoadManager : IDataLoader
{
dataEntities fDb = null;
int count = 0;
public DataLoadManager()
{
fDb = new dataEntities();
fDb.Database.CommandTimeout = 180;
}
public List<spFinance_Result> GetspFinance (DateTime fileDate, int year)
{
try
{
var paramYear = new SqlParameter("@jahr", year);
var paramFileDate = new SqlParameter("@fileDate", fileDate);
var data = fDb.Database.SqlQuery<spFinance_Result>("EXEC spFinance @jahr,@fileDate", paramYear, paramFileDate).ToList();
if (data != null)
{
return data;
}
else
{
return null;
}
}
catch (Exception e)
{
fDb = new dataEntities();
count++;
if (count < 3)
GetspFinance (fileDate, year);
}
return null;
}
}
我已经尝试使用计数器和递归调用来执行上述操作,以便它尝试3次来获取数据。但是它第一次没有得到这个而是超时错误。但是,如果我关闭程序并再次运行,那就好又快。
在前端的呼叫是:
await Task.Run(() =>
{
try
{
FinanceData = financeDataLoader.GetspFinance(fileDate, year);
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message + "/n Please contact with your administrator", "Systom Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
存储过程为:
CREATE PROCEDURE spFinance
@jahr AS INT,
@fileDate AS DATE
AS
BEGIN
SELECT
ROW_NUMBER() OVER (ORDER BY ANSCHLAG) AS [SL],
M.GroupName,
CASE
WHEN F.SMV = 0 THEN M.Zeilenzuordnung
ELSE M.ZeilenzuordnungSMV
END AS Zeilenzuordnung,
F.*
FROM
(SELECT
CASE
WHEN ADVART = 'SOGL' THEN 'VI' ELSE ANSCHLAG
END AS ANSCHLAG,
AUKURZ,
INVENTORY,
ad.INVTRANCHE,
SUM(ABS(ad.AGNETTO2)) AS 'Total',
SUM(CASE WHEN MONAT = 'Januar' THEN AGNETTO2 ELSE 0 END) AS 'JANUAR',
SUM(CASE WHEN MONAT = 'Februar' THEN AGNETTO2 ELSE 0 END) AS 'Februar',
SUM(CASE WHEN MONAT = 'März' THEN AGNETTO2 ELSE 0 END) AS 'März',
SUM(CASE WHEN MONAT = 'April' THEN AGNETTO2 ELSE 0 END) AS 'April',
SUM(CASE WHEN MONAT = 'Mai' THEN AGNETTO2 Else 0 END) AS 'Mai',
SUM(CASE WHEN MONAT = 'Juni' THEN AGNETTO2 Else 0 END) AS 'Juni',
SUM(CASE WHEN MONAT = 'Juli' THEN AGNETTO2 Else 0 END) AS 'Juli',
SUM(CASE WHEN MONAT = 'August' THEN AGNETTO2 Else 0 END) AS 'August',
SUM(CASE WHEN MONAT = 'September' THEN AGNETTO2 Else 0 END) AS 'September',
SUM(CASE WHEN MONAT = 'Oktober' THEN AGNETTO2 Else 0 END) AS 'Oktober',
SUM(CASE WHEN MONAT = 'November' THEN AGNETTO2 Else 0 END) AS 'November',
SUM(CASE WHEN MONAT = 'Dezember' THEN AGNETTO2 Else 0 END) AS 'Dezember',
ad.KUKURZ, ad.KUNDENR, 0 AS [SMV], ad.AU
FROM
[dbo].[anualdata] ad
WHERE
FILEDATE = @fileDate
AND JAHR = @jahr
AND ANSCHLAG NOT IN ('ON' , 'HD', 'TV', 'RA', 'PN')
-- AND KUKURZ = 'BAT'
GROUP BY
ANSCHLAG, AU, AUKURZ, INVENTORY, ad.KUKURZ, ad.KUNDENR, ad.INVTRANCHE, ADVART
UNION ALL
SELECT
CASE WHEN ADVART = 'SOGL' THEN 'VI' ELSE ANSCHLAG END AS ANSCHLAG,
AUKURZ, INVENTORY, ad.INVTRANCHE,
SUM(ABS(ad.SMV)) AS 'Total',
SUM(ABS(CASE WHEN MONAT = 'Januar' THEN SMV ELSE 0 END)) AS 'JANUAR',
SUM(ABS(CASE WHEN MONAT = 'Februar' THEN SMV ELSE 0 END )) AS 'Februar',
SUM(ABS(CASE WHEN MONAT = 'März' THEN SMV ELSE 0 END)) AS 'März',
SUM(ABS(CASE WHEN MONAT = 'April' THEN SMV ELSE 0 END)) AS 'April',
SUM(ABS(CASE WHEN MONAT = 'Mai' THEN SMV ELSE 0 END)) AS 'Mai',
SUM(ABS(CASE WHEN MONAT = 'Juni' THEN SMV ELSE 0 END)) AS 'Juni',
SUM(ABS(CASE WHEN MONAT = 'Juli' THEN SMV ELSE 0 END)) AS 'Juli',
SUM(ABS(CASE WHEN MONAT = 'August' THEN SMV ELSE 0 END)) AS 'August',
SUM(ABS(CASE WHEN MONAT = 'September' THEN SMV ELSE 0 END)) AS 'September',
SUM(ABS(CASE WHEN MONAT = 'Oktober' THEN SMV ELSE 0 END)) AS 'Oktober',
SUM(ABS(CASE WHEN MONAT = 'November' THEN SMV ELSE 0 END)) AS 'November',
SUM(ABS(CASE WHEN MONAT = 'Dezember' THEN SMV ELSE 0 END)) AS 'Dezember',
ad.KUKURZ, ad.KUNDENR, 1 AS [SMV], ad.AU
FROM
[dbo].[anualdata] ad
WHERE
FILEDATE = @fileDate
AND JAHR = @jahr
AND ANSCHLAG NOT IN ('ON', 'HD', 'TV', 'RA', 'PN')
-- AND KUKURZ = 'BAT'
GROUP BY
ANSCHLAG, AU, AUKURZ, INVENTORY, ad.KUKURZ, ad.KUNDENR, ad.INVTRANCHE, ADVART) AS F
LEFT JOIN
(SELECT
mt.ShortName, mg.GroupName, mt.Zeilenzuordnung, mt.ZeilenzuordnungSMV
FROM
[FinanceVolumeData].[dbo].[MediaType] mt
INNER JOIN
[dbo].[MediaGrouppe] mg ON mg.Id = mt.GroupId) AS M ON M.ShortName = F.ANSCHLAG
ORDER BY
[SMV], AUKURZ
END
GO
任何解决方案都会有所帮助。