简体   繁体   中英

SQL Server TimeOut in C# After Adjusting timeout

I have a windows form application written in C# that passes a query to a SQL Server database and then displays the results in a dataviewgrid. The query that is passed to the database depends on the option selected in the form.

One particular query takes a little over a minute to run in management studio, but timeouts when it is passed to the database from the program. here are the steps I have done to try to resolve the situation:

  1. Added a 5 minute timeout in the program by setting the connection timeout option to 300 seconds in the sql connection string. Example: Data Source=ab;Initial Catalog=abc;User ID=user; Password =pw; Connection Timeout=300 Data Source=ab;Initial Catalog=abc;User ID=user; Password =pw; Connection Timeout=300

  2. Setting the remote query timeout in the SQL Server instance to 0 (meaning, no timeout). Example: EXEC sp_configure 'remote query timeout', 0 ; GO EXEC sp_configure 'remote query timeout', 0 ; GO

Neither of these options work. Despite implementing both of them, the c# program throws back a sql timeout error after less than a minute.

Is there a workaround for this? I have searched for this topic on stack overflow and so far all of the suggestions have been to do either 1 or 2 (which I have done).

For reference, I am using Visual Studio 17 Community edition and SQL Server 2016 Developer edition.

Any help would be greatly appreciated.

Thanks!

There's a "CommandTimeout" property on the SQL command. Try setting that.

Connection timeout and command timeout are two different things.

A connection timeout occurs when a connection cannot be retrieved from the connection pool within the allotted timeout period.

A command timeout occurs when a connection has been retrieved, but the query being executed against it doesn't return results within the allotted command timeout period. The default command timeout period in ADO.NET is 30 seconds.

If you've set the connection timeout to 300 seconds and you're still getting a timeout, it's likely a command timeout. As walkers01 said, set your command timeout to a suitable number of seconds. 300 seconds should be far more than sufficient; if your query executes in one minute in SSMS, a timeout of 90 seconds should suffice.

Try this for unlimited time of query execution if you are using SqlDataAdapter. I have use this one, solved my problem.

 SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
                    dscmd.SelectCommand.CommandTimeout = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM