2009-05-07 38 views
1

我使用asp.net,.NET 3.5,C#,和SQL Server Express 2005Asp.Net给出一个超时错误,同时运行一个存储过程

我已经在SQL创建一个存储过程,当我从SQL Server运行SP只需不到1秒即可返回结果。我也在查询分析器中尝试过这个查询,并且它在不到1秒的时间内也给出了结果。但是当我尝试从.NET(C#)调用此SP时,需要很长时间,然后发生超时错误。

这里是我用来调用存储过程的代码:

SqlConnection con = new SqlConnection(); 

con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
con.Open(); 

SqlCommand command = new SqlCommand("spReport_SiteUsage_KP", con); 

command.CommandType = CommandType.StoredProcedure; 

command.Parameters.Add(new SqlParameter("@fromDate", SqlDbType.DateTime)); 

command.Parameters.Add(new SqlParameter("@toDate", SqlDbType.DateTime)); 

command.Parameters[0].Value = Convert.ToDateTime(DatePicker1.SelectedDate.ToShortDateString()); 

command.Parameters[1].Value = DatePicker2.SelectedDate; 

int i = command.ExecuteNonQuery(); 

con.Close(); 

存储过程:

set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[spReport_SiteUsage_KP] 
    @fromDate datetime = null, 
    @toDate datetime = null 
AS 
    truncate table dbo.RPT_SiteUsage 

IF (@FromDate is not null and @ToDate is not null) --Hourly Report, grouped by hour 
Begin 

    insert into RPT_SiteUsage 
    select '' as ReportType, 
    'Site Usage for '+ datename(mm,@fromDate)+' '+datename(dd,@fromDate)+', '+datename(yy,@fromDate) + 
    ' To '+datename(mm,@toDate)+' '+datename(dd,@toDate)+', '+datename(yy,@toDate) as ReportTitle, 
    min(@fromDate) as FromDate,max(@toDate) as ToDate, 
    isnull(count(s.SessionId),0) VisitsTotal, 
    isnull(count(distinct(s.cookieid)),0) VisitsUnique, 
    isnull(sum(PagesVisited),0) PageViews, 
    isnull(round(avg(convert(decimal(10,2),PagesVisited)),2),0) PagePerVisit, 
    isnull(min(PagesVisited),0) MinNoPageViews, 
    isnull(max(PagesVisited),0) MaxNoPageViews, 
    isnull(round(avg(convert(decimal(10,2),TimeInSiteMinutes)),2),0) AvgTimeInSite, 
    isnull(min(TimeInSiteMinutes),0) MinTimeSpent, 
    isnull(max(TimeInSiteMinutes),0) MaxTimeSpent, 
    isnull(sum(NewPeople),0) as NewVisitors 
    from 
    dbo.UMM_UserAction ua inner join dbo.UMM_Session s on ua.SessionId=s.Sessionid 
    left join 
     (select ua.sessionId, datediff(ss,min(Actiondate),max(Actiondate))/60 TimeInSiteMinutes 
     from dbo.UMM_UserAction ua 
     where ActionDate between @fromDate and @toDate 
     group by ua.sessionid 
     ) sessionTime on ua.sessionId = sessionTime.sessionid 
    left join 
     (select ua.sessionId, 0 as NewPeople 
     from dbo.UMM_UserAction ua 
      inner join dbo.UMM_Session s on ua.SessionId=s.SessionId 
      inner join dbo.UMM_Cookie c on s.CookieId=c.CookieId 
      where ua.actiondate< @fromDate --this is the from date 
     group by UserId,ua.sessionId  
     ) Old on ua.sessionId = Old.sessionid 
    left join 
     (select ua.sessionId,count(distinct(uaP.PageEntryId)) as PagesVisited 
     from dbo.UMM_UserAction ua, 
     dbo.UMM_UserActionPageReview uaP 
     where ua.UserActionId=uaP.UserActionId 
     and ActionDate between @fromDate and @toDate 
     group by ua.sessionId 
     )pVisited on ua.sessionId = pVisited.sessionId 
    where ActionDate between @fromDate and @toDate 

    IF (select count(*) from RPT_SiteUsage)=0 
     insert into RPT_SiteUsage 
     select '(1 day)' as ReportType, 
     'Site Usage for '+datename(mm,@fromDate)+' '+datename(dd,@fromDate)+', '+datename(yy,@fromDate) + 
     ' To '+datename(mm,@toDate)+' '+datename(dd,@toDate)+', '+datename(yy,@toDate) as ReportTitle, 
     min(@fromDate) as FromDate,max(@toDate) as ToDate, 
     0 as VisitsTotal, 
     0 as VisitsUnique, 
     0 as PageViews, 
     0 as PagePerVisit, 
     0 as MinNoPageViews, 
     0 as MaxNoPageViews, 
     0 as AvgTimeInSite, 
     0 as MinTimeSpent, 
     0 as MaxTimeSpent, 
     0 as NewVisitors 

END 
+0

发布您的存储过程以及。 – 2009-05-07 16:13:16

+0

在什么行上超时? con.Open(可能)或command.Execute? – 2009-05-07 16:14:35

+0

超时是comminig int i = command.ExecuteNonQuery(); – Kartik 2009-05-07 16:28:31

回答

1

嗯 - 我会说有你的连接字符串中的错误。请检查一下。

+0

但是当我通过小日期范围它工作正常,但是当我通过更多然后3个月然后它给出超时错误..我也尝试在相同的大范围(aprox一年)的SQL,它出来少于一秒。 .. – Kartik 2009-05-07 16:14:22

0

如果在查询返回错误前需要很长时间,那么连接(字符串)可能有问题。

+0

它工作正常,如果我通过小日期范围数据 – Kartik 2009-05-07 16:15:14

+0

啊,更多信息。你能发布异常细节吗? – GvS 2009-05-08 07:46:55

0

这可能是缓存在proc上的坏的查询计划的有趣问题。特别是,如果查询分析器中的查询运行良好,则proc的内核就可以运行。检查这个链接如何解决的情况:http://www.mssqltips.com/tip.asp?tip=1304

+0

没有得到任何运气..你有更多的idiea? – Kartik 2009-05-07 17:03:17

2

另一个想法,每个SqlCommand的TimeOut也是单独控制,所以,你可以用CommandTimeOut属性来控制它。

command.CommandTimeout = 120; 

不过,我会检查执行计划,看看它在哪里浪费或占用DB资源,我认为这只是实验,而不是生产。

+0

我试图做到这一点,它是在2个薄荷糖后出现的,但是它在sql中的时间太多了,它在一秒钟内回复 – Kartik 2009-05-07 17:25:25

相关问题