更改 SqlConnection 超时

本文关键字:超时 SqlConnection 更改 | 更新日期: 2023-09-27 18:30:41

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

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

有没有办法解决这个问题?

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();
    }
}

更改 SqlConnection 超时

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

它的用法是:

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

可以在连接字符串中设置超时值,但在连接后,它将是只读的。您可以在 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx 阅读更多信息

正如 Anil 所暗示的那样,ConnectionTimeout 可能不是您所需要的;它控制 ADO 驱动程序在建立新连接时等待的时间。您的用法似乎表明需要等待比正常时间更长的时间才能执行特定的 SQL 查询,在这种情况下,Anil 完全正确;使用 CommandTimeout(即 R/W)更改单个 SqlCommand 的预期完成时间。

一个更干净的方法是在 xml 文件中设置 connectionString,例如 Web.Confing(WepApplication)App.Config(StandAloneApplication)

 <connectionStrings>
    <remove name="myConn"/>
    <add name="myConn" connectionString="User ID=sa;Password=XXXXX;Initial Catalog=qualitaBorri;Data Source=PC_NAME'SQLEXPRESS;Connection Timeout=60"/>
  </connectionStrings>

通过代码,您可以通过以下方式获得连接:

public static SqlConnection getConnection()
{
        string conn = string.Empty;
        conn = System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
        SqlConnection aConnection = new SqlConnection(conn);
        return aConnection;
}

您只能设置ConnectionTimeout创建实例。创建实例时,请勿更改此值。

始终可以将其添加到连接字符串:

connect timeout=180;

你也可以使用 SqlConnectionStringBuilder

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.ConnectTimeout = 10;
using (var connection = new SqlConnection(builder.ToString()))
{
    // code goes here
}

可以将Connection Timeout=180;添加到连接字符串

旧帖子,但当它出现我正在搜索的内容时,我想我会在这个主题中添加一些信息。我打算添加评论,但我没有足够的代表。

正如其他人所说:

连接。连接超时用于初始连接

命令。命令超时用于单个搜索、更新等。

但:

连接。连接超时用于提交和回滚事务。

是的,这是一个绝对疯狂的设计决定。

因此,如果在提交或回滚时遇到超时,则需要通过连接字符串增加此值。

您可以将连接超时设置为连接级别和命令级别。

将"连接超时 = 10"添加到连接字符串。现在连接超时为 10 秒。

var connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Connection Timeout=10";
using (var con = new SqlConnection(connectionString))
{
}

将 CommandTimeout 属性设置为 SqlCommand

var connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword";
using (var con = new SqlConnection(connectionString))
{
    using (var cmd =new SqlCommand())
    {
        cmd.CommandTimeout = 10;
    }
}

我找到了一篇关于这个主题的优秀博客文章:https://improve.dk/controlling-sqlconnection-timeouts/

基本上,您可以在连接字符串中设置连接超时,如下所示:

Data Source=server;Initial Catalog=databaseUser Id=username;Password=password;Connect Timeout=30

或者,在命令对象上设置连接超时,如下所示:

sqlCommand.CommandTimeout = 30;

请注意,超时时间以秒为单位。

此外,此超时不考虑由于服务器死机或过载等情况而导致的连接丢失。这些最终会触发TCP超时。请参阅博客文章,了解一个很好的扩展示例来处理这个问题。

你需要

使用command.CommandTimeout