Sewder
Sewder

Reputation: 754

Timeout a sql execution C#?

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?

Upvotes: 0

Views: 237

Answers (2)

StingyJack
StingyJack

Reputation: 19479

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.

Upvotes: 1

Breeze
Breeze

Reputation: 2058

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

Upvotes: 0

Related Questions