超时sql执行C#?

时间:2015-10-08 17:20:38

标签: c# visual-studio

应用程序在什么时候实际执行查询?是吗 它是在连接打开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));






            }




        }

我尝试只运行30秒的查询超时。我尝试将连接超时添加到连接字符串但是没有用。还有其他建议吗?我可以尝试一下try catch子句吗?

2 个答案:

答案 0 :(得分:1)

正如梅森指出的那样,Fill方法就是执行命令的方法。

您的数据适配器需要在中间使用此额外语句来增加超时。

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

更多信息here

但是,无法使用超时并允许查询继续。

答案 1 :(得分:0)

查询在adapter.Fill()上执行。要更改命令超时,请将masterquery.CommandTimeout设置为一个值(以秒为单位)或将0设置为无超时