ms sql有可能在查询中设置限制执行超时吗?

时间:2017-07-21 13:45:42

标签: sql sql-server set timeout limit

我想在select查询中指定自己的超时执行。

 e.g.; select timeout(5) * from tableName    (time exceeded, because records will not arrive before 50 seconds)

   or

 e.g.; 
      set timeout(5)
      select * from tableName    (time exceeded, because records will not arrive before 50 seconds)

1 个答案:

答案 0 :(得分:0)

可以在代码中

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx

  using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnStringName"].ToString()))
  using (SqlCommand mySqlCommand = new SqlCommand())
    {
        try
        {
            conn.Open();
            mySqlCommand.Connection = conn;
            mySqlCommand.CommandType = CommandType.StoredProcedure;
            mySqlCommand.CommandText = "getCities";
            mySqlCommand.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ConnectionTimeout"].ToString());

            mySqlCommand.Parameters.Add("@param", SqlDbType.VarChar).Value = param;     
            da.SelectCommand = mySqlCommand;
            da.Fill(ds, "cities");
        }
        catch (Exception ex)
        {                
        }
    } // end using  


------------你可以试试
OPENQUERY(Transact-SQL) http://technet.microsoft.com/en-us/library/ms188427.aspx

检查一下:
连接OPENDATASOURCE http://technet.microsoft.com/en-us/library/ms179856.aspx

连接超时DBPROP_INIT_TIMEOUT连接尝试失败后的超时值。