Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Timing out when Command Timeout = 0

I have a frustrating issue with a query that typically takes between 1.5-2 minutes to run (due to a lack of ability to modify this database, we cannot improve it more than this time). The query times out, despite the Command Timeout property being set to 0 (this is C# code).

Here is the code that executes the query:

public DataTable GetData()
{
    DataTable results = new DataTable();

    try
    {
        using (var sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["SqlConnectionString"].ToString()))
        {
            String command = _query;

            sqlConnection.Open();
            var sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = command;
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.CommandTimeout = 0;
            SqlDataAdapter daM = new SqlDataAdapter(sqlCommand.CommandText, sqlConnection);
            daM.Fill(results);
            sqlConnection.Close();
        }
    }
    catch(Exception e)
    {
        Console.WriteLine("Error " + e.StackTrace);
    }

    Console.WriteLine("Retrieving results for query " + _query);
    Console.WriteLine("Total Results: " + results.Rows.Count);
    return results;
}

I'm not sure where to look for the culprit. Setting a more explicit timeout does nothing, and as I said there's no way to further improve the query that we've been able to find. The connection string has the following parameters:

server =

Integrated Security = SSPI

database =

Connection Timeout = 0

Any advice of where I should look next? We are using Microsoft SQL Server.

like image 531
Marshall Tigerus Avatar asked Sep 08 '15 19:09

Marshall Tigerus


People also ask

How do you set a timeout in SQL query?

Using SQL Server Management StudioIn Object Explorer, right-click a server and select Properties. Click the Connections node. Under Remote server connections, in the Remote query timeout box, type or select a value from 0 through 2,147,483,647 to set the maximum number seconds for SQL Server to wait before timing out.

What causes a SQL query to timeout?

SQL Server will typically show you connection timeouts or operation (query) timeouts. These values are set by the client connecting to the SQL Server. An operation timeout occurs when a command takes too long to complete, and the client raises an error.

What is CommandTimeout in connection string?

The time in seconds to wait for the command to execute. The default is 30 seconds.


2 Answers

You have set sqlCommand.CommandTimeout, but later you've created SqlDataAdapter as

SqlDataAdapter daM = new SqlDataAdapter(sqlCommand.CommandText, sqlConnection)

Here adapter implicitly creates and uses new SqlCommand (not the one you've configured) since you've passed there command text, not instance of SqlCommand.

Use another constructor of SqlDataAdapter and create it like

 SqlDataAdapter daM = new SqlDataAdapter(sqlCommand)
like image 50
Andrey Korneyev Avatar answered Oct 13 '22 00:10

Andrey Korneyev


Set timeout on SqlConnection does not work in your case, you need do it on SqlDataAdapter.

daM.SelectCommand.CommandTimeout = Value;

Google for "How do you change a SqlDataAdapter .CommandTimeout?"

like image 29
MrDywar Avatar answered Oct 13 '22 00:10

MrDywar