0

How do I configure CommandTimeout (not Connect Timeout) from connection string in SQL Server?

I thought it'd be simple to find but apparently not. Below I added CommandTimeout=60. It doesn't seem to break anything, but I have no idea if it's actually working or not (and I can't find doc on this)

Data Source=someplace.com;Initial Catalog=MyDB;CommandTimeout=60;User Id=someID;Password=secret;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3761555
  • 851
  • 10
  • 21
  • 2
    it's per command. see https://stackoverflow.com/questions/1198951/how-to-set-the-query-timeout-from-sql-connection-string – muratgu May 28 '19 at 18:30
  • muratgu thanks i read that post, but I need to figure out a way (If it exists) from connection string, not SqlCommand – user3761555 May 28 '19 at 18:41
  • 2
    @user3761555 You can see all the available properties at [SqlConnectionStringBuilder Class](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder?view=netframework-4.8). There is no command timeout available. Perhaps if you [edit] your question to explain the problem you are trying to solve we could help. – Andrew Morton May 28 '19 at 18:55
  • 5
    You ***CANNOT*** set the command timeout in the connection string. You need to specify it on the `SqlCommand` object at runtime – marc_s May 28 '19 at 19:01
  • Tip: If you want to make the command timeout configurable then place it in a separate entry in the appSettings section of your config, and make the code read the timeout value from there, instead of hard-coding it into your application – ADyson May 28 '19 at 19:07
  • The reason why I didnt just set SqlCommand.CommandTimeout is: My project depends on another util project shared by many. In that util project, we use Dapper. I dont want change in that base util project at this point. Thus, looking for an easy way out: If i can set it in connection string, that'd be perfect. – user3761555 May 28 '19 at 19:15
  • Thanks yall, for the confirmation. – user3761555 May 28 '19 at 19:17

2 Answers2

4

It's probably worth pointing out that setting the default command timeout via the connection string is possible in Microsoft.Data.SqlClient since version ~2.1.0. This package is the open source replacement for System.Data.SqlClient and is available from nuget

If you add e.g. ;Command Timeout=300 to your connection string and receive an error Keyword not supported: Command Timeout' you should check that you truly are using Microsoft.Data.SqlClient, and that its version is greater than 2.1; the question was posed before it was possible, but times have changed

0

Confirm, using the versions of:

  • Microsoft ActiveX Data Objects 6.1 Library
  • Microsoft ActiveX Data Objects Recordset 6.0 Library

The CommandTimeout - when set for the Connection object - works also for the ADO Queries following Connection Open.