C# 更改 SqlConnection 超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10108565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing SqlConnection timeout
提问by Haymak3r
I am trying to override the default SqlConnectiontimeout of 15 seconds and am getting an error saying that the
我正在尝试覆盖SqlConnection15 秒的默认超时,但收到一条错误消息,指出
property or indexer cannot be assigned because it is read only.
无法分配属性或索引器,因为它是只读的。
Is there a way around this?
有没有解决的办法?
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();
}
}
采纳答案by Anil Mathew
If you want to provide a timeout for a particular query, then CommandTimeout is the way forward.
如果您想为特定查询提供超时,则 CommandTimeout 是前进的方向。
Its usage is:
它的用法是:
command.CommandTimeout = 60; //The time in seconds to wait for the command to execute. The default is 30 seconds.
回答by Justin Niessner
You could always add it to your Connection String:
您始终可以将其添加到连接字符串中:
connect timeout=180;
回答by KeithS
You can set the timeout value in the connection string, but after you've connected it's read-only. You can read more at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx
您可以在连接字符串中设置超时值,但连接后它是只读的。您可以在http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx阅读更多信息
As Anil implies, ConnectionTimeout may not be what you need; it controls how long the ADO driver will wait when establishing a new connection. Your usage seems to indicate a need to wait longer than normal for a particular SQL query to execute, and in that case Anil is exactly right; use CommandTimeout (which is R/W) to change the expected completion time for an individual SqlCommand.
正如 Anil 暗示的那样,ConnectionTimeout 可能不是您所需要的;它控制建立新连接时 ADO 驱动程序将等待多长时间。您的用法似乎表明需要等待比正常情况更长的时间才能执行特定的 SQL 查询,在这种情况下 Anil 是完全正确的;使用 CommandTimeout(即 R/W)更改单个 SqlCommand 的预期完成时间。
回答by LewisT
You need to use command.CommandTimeout
你需要使用 command.CommandTimeout
回答by Horev Ivan
You can add Connection Timeout=180;to your connection string
您可以添加Connection Timeout=180;到您的连接字符串
回答by Nick Thompson
Old post but as it comes up for what I was searching for I thought I'd add some information to this topic. I was going to add a comment but I don't have enough rep.
旧帖子,但当它出现我正在搜索的内容时,我想我会为此主题添加一些信息。我打算添加评论,但我没有足够的代表。
As others have said:
正如其他人所说:
connection.ConnectionTimeout is used for the initial connection
connection.ConnectionTimeout 用于初始连接
command.CommandTimeout is used for individual searches, updates, etc.
command.CommandTimeout 用于单独搜索、更新等。
But:
但:
connection.ConnectionTimeout is alsoused for committing and rolling back transactions.
connection.ConnectionTimeout也用于提交和回滚事务。
Yes, this is an absolutely insane design decision.
是的,这绝对是一个疯狂的设计决定。
So, if you are running into a timeout on commit or rollback you'll need to increase this value through the connection string.
因此,如果您在提交或回滚时遇到超时,则需要通过连接字符串增加此值。
回答by daniele3004
A cleaner way is to set connectionString in xml file, for example Web.Confing(WepApplication)or App.Config(StandAloneApplication).
更简洁的方法是在 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>
By code you can get connection in this way:
通过代码,您可以通过这种方式获得连接:
public static SqlConnection getConnection()
{
string conn = string.Empty;
conn = System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
SqlConnection aConnection = new SqlConnection(conn);
return aConnection;
}
You can set ConnectionTimeoutonly you create a instance.
When instance is create you don't change this value.
您可以设置ConnectionTimeout只创建一个实例。创建实例时,您不会更改此值。
回答by Kelvin
You can also use the SqlConnectionStringBuilder
您还可以使用 SqlConnectionStringBuilder
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
builder.ConnectTimeout = 10;
using (var connection = new SqlConnection(builder.ToString()))
{
// code goes here
}
回答by Jakir Hossain
You can set the connection timeout to the connection level and command level.
您可以将连接超时设置为连接级别和命令级别。
Add "Connection Timeout=10" to the connection string. Now connection timeout is 10 seconds.
将“Connection Timeout=10”添加到连接字符串中。现在连接超时是 10 秒。
var connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Connection Timeout=10";
using (var con = new SqlConnection(connectionString))
{
}
Set the of CommandTimeout property to SqlCommand
将 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;
}
}

