6

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.

2
  • Ouch, not being able to modify the database to fix a query that takes that long is challenging. Do you have nonSARGable predicates or joins? What about scalar functions? Sometimes you can still eek some decent performance boost by tweaking the query. Commented Sep 8, 2015 at 19:46
  • the best performance we've had out of it is basically making our own temp database, pulling data into temp tables and indexing those tables....its pretty atrocious really Commented Sep 8, 2015 at 19:53

2 Answers 2

13

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)
Sign up to request clarification or add additional context in comments.

1 Comment

derp....this is what I get for copying and pasting code and not paying attention to it. Thank you
1

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?"

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.