0

At what point does the application actually execute the query? Is it Is it at connection open adapter.fill?

     if (backgroundWorker1.CancellationPending == false)
        {

        try
        {

            superset = new DataSet();


            string[] lines = BranchTBox.Lines;

            for (int i = 0; i < lines.Length; i++)
            {

                if (lines[i].Length == 3)
                {

                    if (qachk.Checked == false)
                    {
                        connectionString = "Driver={IBM DB2 ODBC DRIVER}; Database=" + lines[i] + "; Hostname=" + lines[i] + "." + lines[i] + ".xx; Port = xx; Protocol = xx; Uid=xx; Pwd= xx;";

                    }
                    else
                    {
                        foreach (Control child in panel4.Controls)
                        {
                            if ((child as RadioButton).Checked)
                            {
                                qaserver = child.Text;
                            }
                        }

                        connectionString = "Driver={IBM DB2 ODBC DRIVER}; Database=" + lines[i] + "; Hostname=" + qaserver + ".xx; Port = xx; Protocol = xx; Uid=xx; Pwd= xx;";

                    }





                    connection = new OdbcConnection(connectionString);

                    adapter = new OdbcDataAdapter(masterquery, connection);


                    connection.Open();
                      if ((backgroundWorker1.CancellationPending == false) && (connection.State == ConnectionState.Open))
                      {
                          if (superset != null)
                          {


                              adapter.Fill(superset);
                              superset.Merge(superset);
                              connection.Close();
                          }

                      }






                    //progressBar1.Value = 0;

                }

               if (backgroundWorker1.CancellationPending == false)
                      {
                          if (superset != null)
                          {

                              dataGridView1.Invoke((Action)(() => dataGridView1.DataSource = superset));
                              dataGridView1.Invoke((Action)(() => dataGridView1.DataSource = superset.Tables[0]));


                              timer1.Stop();
                              progressBar1.Invoke((Action)(() => progressBar1.Value = 0));
                              tabControl1.Invoke((Action)(() => tabControl1.SelectedTab = tabPage3));
                          }
               }



                      //  progressBar1.Invoke((Action)(() => progressBar1.Value = 0));






            }




        }

I'm trying to only run a query for 30 seconds the have it timeout. I tried adding the connection timeout to the connection string but that didn't work. Any other suggestions? Can I try a try catch clause?

6
  • 1
    Connection timeout is not the same as command timeout, which is usually expressed as a property of a SqlCommand object (adapter.SelectCommand?) If you post more code, we can help identify. Commented Oct 8, 2015 at 17:24
  • I'm using the IBM DB2 ODBC DRIVER does that make a difference? Commented Oct 8, 2015 at 17:29
  • 1
    What do you mean "have it time out but not stop it"? That doesn't make sense to me. And yes, the Fill method is what actually executes the command in this case. Commented Oct 8, 2015 at 17:30
  • @Sewder - only that you would be using OdbcCommand or OleDbCommand or something other than SqlCommand. It still has the same property, and the driver should honor the value provided. Commented Oct 8, 2015 at 17:33
  • @mason so it's looping through multiple databases so I just wanted it to close out the connection and then loop to the next database connection, I just need to figure out how to cancel the command now. Commented Oct 8, 2015 at 17:39

2 Answers 2

1

As mason points out, the Fill method is what executes the command.

Your data adapter would need this extra statement in the middle to increase the timeout.

adapter = new OdbcDataAdapter(masterquery, connection);
adapter.SelectCommand.Timeout = 60;
connection.Open();

more info here

However, there is no way to use a timeout and allow the query to continue.

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

2 Comments

Thahks! Is there a catch I can do, once it times out? Like show a message box if it does go over 60 seconds?
You can wrap in a try catch, sure. Just catch the correct exception type.
0

The query is executed on adapter.Fill(). To change the command timeout set the masterquery.CommandTimeout to a value (in seconds) or 0 for no timeout

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.