运行sql脚本时VB.NET中的超时错误

时间:2011-01-28 17:27:51

标签: asp.net sql vb.net sql-server-2008

有人可以帮忙吗?我试图通过连接到服务器并使用存储过程在visual studio中创建网格视图。我测试时出错了。有人可以告诉我我能做什么吗?查询本身大约需要7分钟才能完成。数据存储在表格中。我该怎么做才能延长超时时间?

这是我的连接脚本:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>SQL-EDD 4 SERVER</title>
</head>
<body>
   <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
      runat="server">
      <Columns>
        <asp:BoundField DataField="DBNAME" HeaderText="DBNAME" SortExpression="DBNAME" />
        <asp:BoundField DataField="IMGS" HeaderText="IMGS" ReadOnly="True" 
              SortExpression="IMGS" />
          <asp:BoundField DataField="IMG_DATE_UPLOAD" HeaderText="IMG_DATE_UPLOAD" 
              ReadOnly="True" SortExpression="IMG_DATE_UPLOAD" />
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="IMGCTR"
      ConnectionString="<%$ ConnectionStrings:JEFFREYEDD4 %>" 
        SelectCommandType="StoredProcedure" />


    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

SQL代码如下:

SET NOCOUNT ON
DECLARE @DBNAME NVARCHAR(MAX)
DECLARE @DBNAME1 NVARCHAR(MAX)
DECLARE @varSQL NVARCHAR(MAX)

DROP TABLE TEMPIMGCOUNTERSERVER3 
CREATE TABLE TEMPIMGCOUNTERSERVER3 
(DBNAME NVARCHAR(MAX),
IMG_DATE_UPLOAD DATETIME,
IMG_UPLOAD_COUNT INT)

DECLARE DBNAME CURSOR FAST_FORWARD FOR
SELECT [NAME] FROM sys.databases WHERE [name] like 'z%' and [name] not like 'Z1301_TEVA_SINGLE' AND [NAME] NOT LIKE 'Z1016_E-100016R'
AND CREATE_DATE BETWEEN GETDATE()-30 AND GETDATE()
ORDER BY [Name]

OPEN DBNAME

FETCH NEXT FROM DBNAME INTO @DBname
WHILE (@@FETCH_STATUS=0)
BEGIN

SET @DBNAME1=('USE ['+ @DBNAME +']')
EXEC SP_EXECUTESQL  @DBNAME1

    Set @varSQL='INSERT INTO TEMPIMGCOUNTERSERVER3 (DBNAME, IMG_UPLOAD_COUNT, IMG_DATE_UPLOAD) 
    SELECT ''['+@DBNAME+']'' AS DBNAME, SUM(PGCOUNT) AS IMAGES, convert(NVARCHAR(10), CREATED, 101) AS CREATED
        FROM ['+@DBNAME+'].dbo.tbldoc WHERE CREATED BETWEEN ''2011-01-16'' AND ''2011-01-22'' AND PGCOUNT >0 GROUP BY CREATED 
        HAVING SUM(PGCOUNT) IS NOT NULL'    
    EXEC SP_EXECUTESQL @varSQL

    FETCH NEXT FROM DBNAME 
    INTO @DBNAME
    END

    CLOSE DBNAME
    DEALLOCATE DBNAME

    INSERT TEMPIMGCOUNTERSERVER3 (DBNAME, IMG_UPLOAD_COUNT)  
    SELECT 'TOTAL_IMGSIZE_IN_MBS', SUM(IMG_UPLOAD_COUNT) FROM TEMPIMGCOUNTERSERVER3 

    SELECT  DBNAME, SUM(IMG_UPLOAD_COUNT) AS IMGS, convert(NVARCHAR(10), IMG_DATE_UPLOAD, 101) AS IMG_DATE_UPLOAD FROM TEMPIMGCOUNTERSERVER3 where IMG_UPLOAD_COUNT > 0
    GROUP BY DBNAME, IMG_DATE_UPLOAD
    ORDER BY IMG_DATE_UPLOAD 

错误如下:

服务器错误。

超时已过期。操作完成之前经过的超时时间或服务器没有响应。 该语句已终止。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。 声明已经终止。

来源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

堆栈追踪:

[SqlException(0x80131904):超时已过期。操作完成之前经过的超时时间或服务器没有响应。 该语句已终止。]    System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection)+1951450    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection)+4849003    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)+194    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2394    System.Data.SqlClient.SqlDataReader.ConsumeMetaData()+ 33    System.Data.SqlClient.SqlDataReader.get_MetaData()+ 83    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)+297    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)+954    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)+162    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method)+32    System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)+141    System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)+12    System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)+10    System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable [] datatables,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,CommandBehavior行为)+130    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand command,CommandBehavior behavior)+287    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,String srcTable)+92    System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)+1297    System.Web.UI.DataSourceView.Select(DataSourceSelectArguments参数,DataSourceViewSelectCallback回调)+19    System.Web.UI.WebControls.DataBoundControl.PerformSelect()+ 142    System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+73    System.Web.UI.WebControls.GridView.DataBind()+ 4    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()+82    System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()+72    System.Web.UI.Control.EnsureChildControls()+87    System.Web.UI.Control.PreRenderRecursiveInternal()+ 44    System.Web.UI.Control.PreRenderRecursiveInternal()+171    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+842


版本信息:Microsoft .NET Framework版本:2.0.50727.4952; ASP.NET版本:2.0.50727.4955

3 个答案:

答案 0 :(得分:4)

首先 - 延长超时不是解决方案。不过,以下是您使用数据源的方法:

protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.CommandTimeout = 30;
} 

以下是您应该执行的分析:

首先:当在页面上下文之外执行时,例如在查询窗口中,存储过程是否令人满意地执行?如果没有,问题出在查询上。谷歌“优化SQL查询”,你会得到一堆好东西。

如果是,那么应用程序中的其他查询如何执行(假设这不是您尝试使用的第一个查询)?如果它们正常工作,请仔细检查您是否使用相同的连接字符串。

答案 1 :(得分:1)

为了增加超时,您可以为正在获取数据的DataCommand增加CommandTimeout。使用SqlDataSource时,使用事件处理程序完成此操作以获取命令。 在页面后面的代码中插入以下内容:

Private Sub SqlDataSource1_Selecting(sender as object, _
   e as SqlDataSourceSelectingEventArgs) handles SqlDataSource1.Selecting

   e.Command.CommandTimeout = 500 
End Sub

为了增加Web应用程序的请求超时,您必须通过修改Web配置将executionTimeout设置为合适的长度。

<httpRuntime
   executionTimeout = "600" 
/>

但更好的是调整你的查询。

答案 2 :(得分:0)

所以这里有一种方法,你可能想看看光标。我没有运行它,但通过一些调整它应该工作。

CREATE Table #TableNames (DBNameID INT Not NUll Identity, DBName VarCHAR(50))

CREATE TABLE #TableResults (DBNAME NVARCHAR(MAX),  IMG_DATE_UPLOAD DATETIME,  IMG_UPLOAD_COUNT INT)

Insert Into #TableNames(DBName)
SELECT [NAME] FROM sys.databases 
WHERE [name] like 'z%' and [name] not like 'Z1301_TEVA_SINGLE' 
AND [NAME] NOT LIKE 'Z1016_E-100016R' 
AND CREATE_DATE BETWEEN GETDATE()-30 AND GETDATE() ORDER BY [Name]

DECLARE @Counter INT = 1;
DECLARE @DBName VARCHAR(MAX)
DECLARE @varSQL NVARCHAR(MAX)
Declare @rows INT;
select @rows = COUNT(DBNameID) from #TableNames


While @Counter <= @rows BEGIN
    SET @DBName = (SELECT DBName from #TableNames where DBNameID = @Counter)

    Set @varSQL='INSERT INTO #TableResults (DBNAME, IMG_UPLOAD_COUNT, IMG_DATE_UPLOAD)      
SELECT ''['+@DBNAME+']'' AS DBNAME, SUM(PGCOUNT) AS IMAGES, convert(NVARCHAR(10), CREATED, 101) AS CREATED
FROM ['+@DBNAME+'].dbo.tbldoc WHERE CREATED BETWEEN ''2011-01-16'' AND ''2011-01-22'' AND PGCOUNT >0 GROUP BY CREATED          
HAVING SUM(PGCOUNT) IS NOT NULL' 

SET @Counter += 1
END

INSERT #TableResults (DBNAME, IMG_UPLOAD_COUNT)       
SELECT 'TOTAL_IMGSIZE_IN_MBS', SUM(IMG_UPLOAD_COUNT) 
FROM #TableResults       

SELECT  DBNAME, SUM(IMG_UPLOAD_COUNT) AS IMGS, convert(NVARCHAR(10), IMG_DATE_UPLOAD, 101) AS IMG_DATE_UPLOAD 
FROM #TableResults where IMG_UPLOAD_COUNT > 0     
GROUP BY DBNAME, IMG_DATE_UPLOAD     
ORDER BY IMG_DATE_UPLOAD

DROP TABLE #TableNames
DROP TABLE #TableResults

如果你正在使用它仍然花费太长时间,你可能想看看运行每个数据库正在执行的选择所需的时间。如果每个都花费很长时间,您可能需要查看查询本身的那部分的查询计划。