超时过期错误

时间:2015-08-18 13:55:15

标签: c# sql asp.net

我的项目中有一个网页,RadGrid在其中显示多个记录。有时网页工作正常,但有时会显示错误 -

enter image description here

在上面的快照中,[Invoice].[ups_tbl_Request_Query_Select_AdminView]是存储过程的名称 修改
存储过程:

ALTER PROCEDURE [Invoice].[usp_tbl_Request_Query_Select_AdminView]
    -- Add the parameters for the stored procedure here

    @Status tinyint = Null,
    @SearchValue nvarchar(50) = Null

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

    If (LTRIM(@SearchValue) = '')
        Set @SearchValue = Null

    Select Distinct
        R.RequestID,
        R.RequisitionNo,
        R.BarcodeNo,
        R.IsThreeWayMatch,
        R.IsUrgent,
        R.CreatedBy,
        R.TotalAmount,
        R.TotalInvoiceAmount,
        R.CompanyCode as BuyerNo,
        W.Action,
        W.CreatedDate,
        --R.CreatedDate,
        R.SubmissionDate,
        CASE
        WHEN (R.IsThreeWayMatch in (1))
            THEN X.BuyerName   
        ELSE R.Company   
         END AS BuyerName,

        X.SupplierNo,
        X.SupplierName,
        X.InvoiceNo,
        S.RequestStatus,    
        --w.UserName,
        --(select top 1 w.CreatedDate from Invoice.tbl_WorkflowHistory W where w.CreatedBy = R.CreatedBy and R.RequestID = W.RequestID order by w.CreatedDate desc) as date1,
        (select top 1 w.UserName from Invoice.tbl_WorkflowHistory (NOLOCK) W where w.CreatedBy = R.CreatedBy and R.RequestID = W.RequestID order by w.CreatedDate desc) as UserName

    INTO #Records1

    From Invoice.tbl_Request (NOLOCK) R
        Left Join Invoice.tbl_Xml(NOLOCK) X On X.XmlID = R.XmlID
        Left Join Invoice.tbl_WorkflowHistory (NOLOCK) W On W.RequestID = R.RequestID
        Left Join Invoice.tbl_RequestStatus (NOLOCK) S On S.RequestStatusNo = R.[Status]
    Where ((IsNull(R.RequisitionNo, '') Like '%' + Coalesce(@SearchValue, R.RequisitionNo, '') + '%')
            Or (IsNull(R.BarcodeNo, '') Like '%' + Coalesce(@SearchValue, R.BarcodeNo, '') + '%')
            --Or (IsNull(X.BuyerNo, '') Like '%' + Coalesce(@SearchValue, X.BuyerNo, '') + '%')
            --Or (IsNull(X.SupplierNo, '') Like '%' + Coalesce(@SearchValue, X.SupplierNo, '') + '%')
            Or (IsNull(R.Company, '') Like '%' + Coalesce(@SearchValue, R.Company, '') + '%')
            Or (IsNull(X.SupplierName, '') Like '%' + Coalesce(@SearchValue, X.SupplierName, '') + '%'))
        --And (W.[Role] = 'AP Admin'
        --  Or W.[Role] = 'AP Associate')
        And R.[Status] = IsNull(@Status ,R.[Status]) 
    Order By R.RequisitionNo


    SELECT * FROM 
    (
    SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY RequisitionNo ORDER BY CreatedDate DESC) RowNumber FROM #Records1 ) AS t
    WHERE RowNumber = 1

    ) AS a Order By RequisitionNo

END

HTML code:

<asp:ObjectDataSource ID="odsDocumentHistoryAdmin" runat="server" SelectMethod="Query_Test" TypeName="atQuest.Projects.Sunway.IPRRequest" SortParameterName="orderBy">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="ddlStatus" Name="status" PropertyName="SelectedValue"
                                Type="Int32" ConvertEmptyStringToNull="true"  />
                            <asp:ControlParameter ControlID="txtSearch" Name="searchValue" PropertyName="Text"
                                Type="String" ConvertEmptyStringToNull="true" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                    <telerik:RadGrid ID="qgDocumentHistoryAdmin" runat="server" DataSourceID="odsDocumentHistoryAdmin"
                                AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True"
                                CellSpacing="0" GridLines="None" Width="100%" Skin="Outlook">
                                <ClientSettings AllowDragToGroup="True" />
                                <GroupingSettings CaseSensitive="false"></GroupingSettings>
                                <MasterTableView AllowFilteringByColumn="true" AllowMultiColumnSorting="false" AutoGenerateColumns="false"
                                    CommandItemDisplay="Top" DataKeyNames="RequisitionNo" EnableGroupsExpandAll="true"
                                    GroupLoadMode="Client" PageSize="50">
                                    <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" />
                                    <SortExpressions>
                                        <telerik:GridSortExpression FieldName="RequisitionNo" SortOrder="Descending" />
                                    </SortExpressions>
                                    <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" Position="Bottom" PageSizes="50,100,150,200" />
                                    <Columns>
                                        //all GridBound columnc
                                    </Columns>
                                </MasterTableView>
                                <ExportSettings SuppressColumnDataFormatStrings="True" IgnorePaging="True" ExportOnlyData="True" Excel-Format="ExcelML" OpenInNewWindow="True" FileName="eAPDocHistory" Excel-FileExtension="xls">
                                </ExportSettings>
                            </telerik:RadGrid>

班级文件代码:

[DataObjectMethod(DataObjectMethodType.Select, true)]
        public static IPRRequests Query_Test(int? status, string searchValue, string orderBy)
        {
            IPRRequests iPRRequests = new IPRRequests();
            Data data = new Data();
            Dictionary<string, object> input = new Dictionary<string, object>()
            {
                {"@Status", status},
                {"@SearchValue", searchValue}
            };
            DataTable dt = data.ExecuteQuery(CommandType.StoredProcedure, "[Invoice].[usp_tbl_Request_Query_Select_AdminView]", input);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;
                if (!string.IsNullOrEmpty(orderBy))
                {
                    string colName = orderBy.Replace(" DESC", "");
                    try
                    {
                        //dv.Sort = orderBy.Replace(colName, FieldMapping[colName.Trim()]);
                        dv.Sort = orderBy;
                    }
                    catch (Exception)
                    {
                        throw new Exception("orderby:" + orderBy + ";colname:" + colName);
                    }

                    dt = dv.ToTable();
                }

                foreach (DataRow dr in dt.Rows)
                {
                    iPRRequests.Add(new IPRRequest(dr, true));
                }
                dt.Dispose();
                dv.Dispose();
            }

            return iPRRequests;
        }

我无法理解大多数时间发生此错误的原因,但并非总是如此。
请让我知道这个问题的原因,如何以简单的方式解决它。
请回复。

4 个答案:

答案 0 :(得分:1)

您正在加载的视图有很多记录,这会导致超时,因为没有足够的时间加载。最简单的解决方案是增加超时。但我不建议采用这种方法。

执行此操作的明智方法是仅加载可见的记录。我给你举个例子。页面打开时,您只能看到20条记录。所以你的查询应该只加载20条记录。您可以查看SQL RowNumber。在本文中,您感兴趣的是返回行的子集

以下是

的示例
SELECT * FROM
    (SELECT ROW_NUMBER() 
        OVER (ORDER BY EmployeeName) AS Row, 
        EmployeeId, EmployeeName, Salary 
    FROM Employees) AS EMP
WHERE Row BETWEEN 20 AND 40

这是一个如何仅显示特定记录的简单示例。因此,如果您每页有20条记录而且您在第2页,则会有这样的查询。

答案 1 :(得分:0)

您应该查看您的查询,了解为何需要这么长时间。有时您的查询运行时间超过默认命令超时30秒。

SqlCommand.CommandTimeout Property的默认命令超时为30秒。您可以尝试将其增加到60秒。

以下代码取自MSDN网站。

 using (SqlConnection connection = new SqlConnection(connectionString))
 {
     connection.Open();
     SqlCommand command = new SqlCommand(queryString, connection);
     // Setting command timeout to 60 second
     command.CommandTimeout = 60;
     try {
         command.ExecuteNonQuery();
     }
     catch (SqlException e) {
         Console.WriteLine("Got expected SqlException due to command timeout ");
         Console.WriteLine(e);
     }
}

答案 2 :(得分:0)

如果您使用的是SqlCommand对象,请按以下方式设置:

Pfn buttonOnTick = [](void* ctxt, int i){
    static_cast<Button*>(ctxt)->onTick(i);
};

看看是否能解决你的问题。如果是这样,您可以将其保留,或者将值更改为您觉得足够大的值。

答案 3 :(得分:0)

这基本上是数据库不与网页通信。如果您的数据库在线,则可能存在连接字符串问题或者您的网络连接速度很慢