简体   繁体   English

超时的SQL执行C#?

[英]Timeout a sql execution C#?

At what point does the application actually execute the query? 应用程序在什么时候实际执行查询? Is it Is it at connection open adapter.fill? 是在连接打开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. 我试图只运行30秒钟的查询使其超时。 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? 我可以尝试try catch子句吗?

As mason points out, the Fill method is what executes the command. 正如梅森指出的那样,Fill方法是执行命令的地方。

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. 但是,无法使用超时并允许查询继续。

The query is executed on adapter.Fill() . 该查询在adapter.Fill()上执行。 To change the command timeout set the masterquery.CommandTimeout to a value (in seconds) or 0 for no timeout 要更改命令超时,请将masterquery.CommandTimeout设置为一个值(以秒为单位)或0表示没有超时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM