【问题标题】:SqlDataAdapter object CommandTimeOut does not work in C#SqlDataAdapter 对象 CommandTimeOut 在 C# 中不起作用
【发布时间】:2019-11-07 19:37:37
【问题描述】:

我正在使用 SqlDataAdapter 从存储过程中提取结果,该过程最多需要 5 分钟才能执行并返回结果。

我正在使用一个

da.SelectCommand.CommandTimeout = 1800;

设置,但超时不起作用。该代码实际上并未兑现超时。它更早失败了。

知道如何解决这个超时问题吗?

这是我的代码:

var cpdbconn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL"].ConnectionString);   

using (SqlCommand cmd = new SqlCommand()) 
{
    cmd.Connection = cpdbconnection; 
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = readStoredProcedureName;

    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
    {
        try
        {
            da.SelectCommand.CommandTimeout = 1800;
            da.Fill(dt);

            // Check datatable is null or not
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dataRow in dt.Rows)
                {
                    lstring.Add(Convert.ToString(dataRow["ServerName"]));
                }
            }

            // Add "','" in each row to convert the result to support nested query format
            InnerQryResultStr = string.Join("','", lstring.ToArray());

            if (multinestedQry != null)
            {
                combinedQry = qryName;
                qryName = multinestedQry + "('" + InnerQryResultStr + "')";
            }
            else
            {
                qryName = qryName + "('" + InnerQryResultStr + "')";
            }
        }
        catch (SqlException e)
        {
            Logger.Log(LOGTYPE.Error, String.Format("Inserting Data Failed for server {0} with Exception {1}", "DiscreteServerData", e.Message));

            if(e.Number == -2)
            {
                Logger.Log(LOGTYPE.Error, String.Format("TimeOut occurred while executing SQL query / stored procedure ", "DiscreteServerData", e.Message));
            }

            strMsg = e.Message.ToString();
            file.WriteLine(strMsg.ToString());
        }
    }
}

【问题讨论】:

  • "存储过程执行结果最多需要 5 分钟。"你在用 5 分钟 执行的可怜的数据库做什么?正常响应时间以毫秒为单位。为了避免这 5 分钟,可能需要进行轻微的重新设计。
  • 还有一堆超时。 CommandTimeout、Connection Timeout、对端服务器超时、Slow Lorris检测等
  • StoredProdcuede 产生数百万条记录。所以预计 SP 的执行时间

标签: c# sql-server sqldataadapter


【解决方案1】:

您是否尝试在 SqlCommand 上设置超时? cmd.CommandTimeout = 300;

【讨论】:

  • 在连接字符串和上面的行中添加超时帮助我解决了这个问题
【解决方案2】:

对于数据库操作,需要考虑一大堆超时:

Command has a timeout

connection has a timeout.

网络部分的每一层都有超时。

另一端的服务器超时。

另一端的服务器可能有slow loris protection

锁和事务可能会超时(因为在某些时候,其他人可能也想使用该表)。

根据您的 cmets,您的存储过程返回“数百万条记录”。 Wich 可能是问题所在。这不是一个可用的查询。数据量如此之大,超过了单纯的网络或数据库问题 - 并且可能成为客户端的内存问题。

检索太多是一个常见的错误,即使我记不起这个规模的任何事情(最大的在 100k 中)。用户没有办法处理这种信息,所以必须有更多的过滤、分页等。永远不要在客户端中执行这些步骤。充其量您通过网络传输无用的数据量。在最坏的情况下,您会遇到超时和并发问题。始终在查询中执行尽可能多的过滤、分页等操作。

您希望总体上检索尽可能少的数据。合并、备份等批量操作通常应在 DBMS 中完成。如果将其移至客户端,则只会为数据添加另一层故障和两种联网方式。

为了获得更好的答案,我需要更准确的问题信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多