SqlDataAdapter.Fill() 超时 - 底层 Sproc 快速返回

5 sql timeout dataadapter fill

我有一个 SqlDataAdapter,它填充了 21 行数据(4 列)。驱动它的 sproc 在几秒钟内在 SQL Mgmt Studio 中返回,但 .Fill() 需要 5 分钟。

    ArrayList ret = new ArrayList();
    SqlDataAdapter da = null;
    SqlCommand cmd = null;  
        cmd = base.GetStoredProc("usp_dsp_Stuff"); //Returns immediately in MSSMS.
        cmd.CommandTimeout = 3600; // Set to 6 min - debug only
        base.AddParameter(ref cmd, "@Param1", ParameterDirection.Input, SqlDbType.BigInt, 8, 19, 0, theParam1);
        base.AddParameter(ref cmd, "@Param2", ParameterDirection.Input, SqlDbType.BigInt, 8, 19, 0, theParam2);
        base.AddParameter(ref cmd, "@Param3", ParameterDirection.Input, SqlDbType.Char, 1, 'C');
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt); //Takes 5 minutes.
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

提前致谢!-克里斯

Huy*_*ang 6

最近,我就经历了这样的情况:.Fill超时,但相同的 SP 在 SQL Server Management Studio 中速度超快。这是因为您的 .NET 应用程序创建 SQL 连接并使用,而 SQL Server Management Studio默认SET ARITHABORT OFF使用。SET ARITHABORT ON这会导致使用两个不同的执行计划,因此您无法在 SQL Server Management Studio 中重现此超时。我建议您检查一下您的 SP 并进行一些更改。


小智 5

da = new SqlDataAdapter(cmd);
da.SelectCommand.CommandTimeout = 1800;
Run Code Online (Sandbox Code Playgroud)


cod*_*ike 0

Fill() 有时会很慢,因为 .NET 正在分析从过程返回的数据。

使用 SQL Profiler 计算出执行 Fill() 时 SQL .NET 实际发送的内容。

如果它发送大量 SET 语句,例如

设置 concat_null_yields_null 为 on
将cursor_close_on_commit设置为关闭
将implicit_transactions设置为关闭

ETC...

..然后将这些相同的集合语句放入存储过程中可能会加快速度。