更改SqlConnection超时 [英] Changing SqlConnection timeout

查看:320
本文介绍了更改SqlConnection超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖默认的 SqlConnection 超时(15秒),并收到一条错误消息,指出

I am trying to override the default SqlConnection timeout of 15 seconds and am getting an error saying that the


属性或索引器无法分配,因为它是只读的。

property or indexer cannot be assigned because it is read only.

有没有解决的办法?

using (SqlConnection connection = new SqlConnection(Database.EstimatorConnection))
{
   connection.Open();

   using (SqlCommand command = connection.CreateCommand())
   {
       command.CommandType = CommandType.StoredProcedure;
       connection.ConnectionTimeout = 180; // This is not working 
       command.CommandText = "sproc_StoreData";
       command.Parameters.AddWithValue("@TaskPlanID", order.Projects[0].TaskPlanID);
       command.Parameters.AddWithValue("@AsOfDate", order.IncurDate);

       command.ExecuteNonQuery();
    }
}


推荐答案

如果您要为特定查询提供超时,那么CommandTimeout是前进的方向。

If you want to provide a timeout for a particular query, then CommandTimeout is the way forward.

它的用法是:

command.CommandTimeout = 60; //The time in seconds to wait for the command to execute. The default is 30 seconds.

这篇关于更改SqlConnection超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆