3

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;
7
  • 2
    it's per command. see stackoverflow.com/questions/1198951/… Commented May 28, 2019 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 Commented May 28, 2019 at 18:41
  • 2
    @user3761555 You can see all the available properties at SqlConnectionStringBuilder Class. There is no command timeout available. Perhaps if you edit your question to explain the problem you are trying to solve we could help. Commented May 28, 2019 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 Commented May 28, 2019 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 Commented May 28, 2019 at 19:07

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.