超时设置为SQL Server [英] Timeout setting for SQL Server

查看:217
本文介绍了超时设置为SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VSTS 2008 + ADO.Net + C#+净3.5 + SQL Server 2008中,我使用ADO.Net在客户端连接到数据库服务器执行一个存储过程,然后从存储过程返回结果

下面是我的code。我有两个问题,关于超时,

  1. 如果我没有明确设置任何超时相关的设置,用于连接到数据库服务器,是否有任何超时设置(例如,如果无法连接到数据库服务器的时间了一些默认量,会有一些超时例外?)?

  2. 如果我没有明确设置任何超时相关的设置,对于存储过程的执行,有没有超时设置(例如,如果无法检索服务器的结果ADO.Net客户端的某个时间默认量,会有一些超时异常?)?

     使用(SqlConnection的currentConnection =新的SqlConnection(数据源=;初始目录= TestDB的; Trusted_Connection = TRUE;异步处理=真正的))
        {
            //检查当前批处理conut
            currentConnection.Open();
            使用(SqlCommand的RetrieveOrderCommand =新的SqlCommand())
            {
                RetrieveOrderCommand.Connection = currentConnection;
                RetrieveOrderCommand.CommandType = CommandType.StoredProcedure;
                RetrieveOrderCommand.CommandText =prc_GetOrders;
                RetrieveBatchCountCommand.Parameters.Add(@伯爵,SqlDbType.Int).Direction = ParameterDirection.Output;
                RetrieveBatchCountCommand.ExecuteNonQuery();
                INT rowCount等= Convert.ToInt32(RetrieveOrderCommand.Parameters [@计]值。);
            }
        }
     

解决方案

正如GBN已经提到的,有两种类型的超时:

1)连接超时:这是由你的连接字符串控制:

 数据源=;初始目录= TestDB的;
   Trusted_Connection = TRUE;异步处理=真
 

如果您添加连接超时= 120 来这个字符串,你的连接将尝试为120秒,得到打开,然后中止。

 数据源=;初始目录= TestDB的;
   Trusted_Connection = TRUE;异步处理=真;
   连接超时= 120;
 

2)命令超时时间:每一个命令,你也可以指定一个超时 - ADO.NET将等待的时间量抵消了您的查询之前。您指定的SqlCommand对象上:

 使用(SqlCommand的RetrieveOrderCommand =新的SqlCommand())
    {
       RetrieveOrderCommand.CommandTimeout = 150;
    }
 

I am using VSTS 2008 + ADO.Net + C# + .Net 3.5 + SQL Server 2008. I am using ADO.Net at client side to connect to database server to execute a store procedure, then return result from the store procedure.

Here is my code. I have two issues about timeout,

  1. If I do not explicitly set any timeout related settings, for the connection to database server, are there any timeout settings (e.g. if can not connect to database server for some default amount of time, there will be some timeout exception?)?

  2. If I do not explicitly set any timeout related settings, for the execution of the store procedure, are there any timeout settings (e.g. if can not retrieve results from server to ADO.Net client for some default amount of time, there will be some timeout exception?)?

        using (SqlConnection currentConnection = new SqlConnection("Data Source=.;Initial Catalog=TestDB;Trusted_Connection=true;Asynchronous Processing=true"))
        {
            // check current batch conut
            currentConnection.Open();
            using (SqlCommand RetrieveOrderCommand = new SqlCommand())
            {
                RetrieveOrderCommand.Connection = currentConnection;
                RetrieveOrderCommand.CommandType = CommandType.StoredProcedure;
                RetrieveOrderCommand.CommandText = "prc_GetOrders";
                RetrieveBatchCountCommand.Parameters.Add("@Count", SqlDbType.Int).Direction = ParameterDirection.Output;
                RetrieveBatchCountCommand.ExecuteNonQuery();
                int rowCount = Convert.ToInt32(RetrieveOrderCommand.Parameters["@Count"].Value);
            }
        }
    

解决方案

As gbn already mentioned, there are two types of timeouts:

1) Connection Timeout: this is controlled by your connection string:

Data Source=.;Initial Catalog=TestDB;
   Trusted_Connection=true;Asynchronous Processing=true

If you add a Connect Timeout=120 to this string, you're connection will try for 120 seconds to get opened and then aborts.

Data Source=.;Initial Catalog=TestDB;
   Trusted_Connection=true;Asynchronous Processing=true;
   Connect Timeout=120;

2) Command timeout: for each command, you can also specify a timeout - ADO.NET will wait for that amount of time before cancelling out your query. You specify that on the SqlCommand object:

    using (SqlCommand RetrieveOrderCommand = new SqlCommand())
    {
       RetrieveOrderCommand.CommandTimeout = 150;
    }

这篇关于超时设置为SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆