Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

c# - Timeout setting for SQL Server

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);
            }
        }
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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, your 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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...