Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单SQL查询超时取决于输入参数_C#_Sql_Sql Server_Database_System.data - Fatal编程技术网

C# 简单SQL查询超时取决于输入参数

C# 简单SQL查询超时取决于输入参数,c#,sql,sql-server,database,system.data,C#,Sql,Sql Server,Database,System.data,我有一个非常奇怪的问题,根据查询参数,下面代码中的数据库调用将失败: Exception.Message = Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Exp

我有一个非常奇怪的问题,根据查询参数,下面代码中的数据库调用将失败:

Exception.Message = Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
代码是:

        public IList<ProductCommissionMinimal> FindProductCommissionMinimal(string premiumTypeCode, string sellerId, string eposProductId, string singlePremiumPolicy)
        {
            var sql = @"
SELECT distinct
        CSP.ProductSubtypeId, PC.ProductFamilyId, PC.ProductId
    FROM
        ProductCommission PC,
        CommissionTemplate CT,
        Seller S,
        Product P,
        CoreSystemProduct CSP
    WHERE
        PC.ProductFamilyId = CSP.ProductFamilyId AND PC.ProductId = CSP.ProductId
        AND P.ProductFamilyId = CSP.ProductFamilyId AND P.ProductId = CSP.ProductId
        AND CT.PremiumTypeCd = @premiumTypeId
        AND S.SellerId = @sellerId
        AND CT.CommissionTemplateSeq = PC.CommissionTemplateSeq
        AND CT.StatusCd = 'O'
        AND PC.DistributionId = S.DistributionId
        AND CT.AvailabilityCd <> 'I'
        AND CSP.CoreProductId = @eposProductId";

            var parameters = new Dictionary<string, object>
            {
                { "@premiumTypeId", premiumTypeCode },
                { "@sellerId", sellerId },
                { "@eposProductId", eposProductId },
            };

            if (premiumTypeCode == "1")
            {
                sql = sql + " AND P.IsSinglePremiumOnlyCd = @singlePremiumPolicy";
                parameters["@singlePremiumPolicy"] = singlePremiumPolicy;
            }

            return Query<ProductCommissionMinimal>(sql, parameters);
        }

按照建议。但是,对于某些参数组合,例如premiumTypeCode=1&sellerId=TC99&eposProductId=UPO2SAA&singlePremiumPolicy=N,这仍然是失败的。您是否尝试直接在sql management studio中运行查询?只是为了检查持续时间?另外,我不是一个了不起的SQL专家,但也许你应该在这里使用连接?@Rafalon是的,我这样做了。在那里工作非常好。它几乎立即返回。在那个测试中,我只是在查询中直接用单引号替换参数值。我同意可以改进查询,但从理论上讲,它应该可以工作,并且适用于大多数情况。您应该使用自1992年以来存在的适当的
join
语法构造查询。您是否尝试过执行
dbcc freeproccache
并重新运行?
    public IList<ProductCommissionMinimal> FindProductCommissionMinimal(string premiumTypeCode, string sellerId, string eposProductId, string singlePremiumPolicy)
    {
        var sql = @"
SELECT distinct CSP.ProductSubtypeId, PC.ProductFamilyId, PC.ProductId
                    FROM
                        ProductCommission PC
                    INNER JOIN
                           CommissionTemplate CT 
                    ON 
                           CT.CommissionTemplateSeq = PC.CommissionTemplateSeq
                    INNER JOIN
                        Seller S 
                    ON 
                           PC.DistributionId = s.DistributionId
                    INNER JOIN
                        CoreSystemProduct CSP 
                    ON  
                           PC.ProductFamilyId = CSP.ProductFamilyId AND PC.ProductId = CSP.ProductId
                    INNER JOIN
                        Product P 
                    ON 
                           P.ProductId = CSP.ProductId AND P.ProductFamilyId = CSP.ProductFamilyId  
                    WHERE
                        CT.PremiumTypeCd = @premiumTypeId
                        AND S.SellerId = @sellerId       
                        AND CT.StatusCd = 'O'
                        AND PC.DistributionId = S.DistributionId
                        AND CT.AvailabilityCd <> 'I'
                        AND CSP.CoreProductId = @eposProductId";

        var parameters = new Dictionary<string, object>
        {
            { "@premiumTypeId", premiumTypeCode },
            { "@sellerId", sellerId },
            { "@eposProductId", eposProductId },
        };

        if (premiumTypeCode == "1")
        {
            sql = sql + " AND P.IsSinglePremiumOnlyCd = @singlePremiumPolicy";
            parameters["@singlePremiumPolicy"] = singlePremiumPolicy;
        }

        return Query<ProductCommissionMinimal>(sql, parameters);
    }