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(); } }
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.
The default is 30 seconds.
Answers. Yes. When the using block ends, the connection automatically closes (That is what IDisposable is for). So, do not close the connection explicitly.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With