Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing SqlConnection timeout

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.

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();     } } 
like image 917
Haymak3r Avatar asked Apr 11 '12 14:04

Haymak3r


People also ask

How do I change timeout in SqlConnection?

You can set the amount of time a connection waits to time out by using the Connect Timeout or Connection Timeout keywords in the connection string. A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

What is the default connection timeout for SqlConnection?

The default is 30 seconds.

Does using automatically close SqlConnection?

Answers. Yes. When the using block ends, the connection automatically closes (That is what IDisposable is for). So, do not close the connection explicitly.

How do I change the command timeout in SQL?

Select Query Execution from tree on left side and enter command timeout in "Execute Timeout" control. Changing Command Timeout in Server: In the object browser tree right click on the server which give you timeout and select "Properties" from context menu. you can set the value in up/down control.


1 Answers

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

Its usage is:

command.CommandTimeout = 60; //The time in seconds to wait for the command to execute. The default is 30 seconds. 
like image 76
Anil Mathew Avatar answered Oct 21 '22 02:10

Anil Mathew