简体   繁体   中英

Connection Timeout=0 on Windows Server 2008 R2

Ok, I have a program written in C# that gets some information from the SQL Server 2008 in a series of stored procedures calls. My connection string:

Data Source={0};Initial Catalog={1}; Integrated Security=True; Connection Timeout=0

{0} and {1} are filled by variables.
Timeout is zero because the information can be huge and it might take some time to get it.
The first procedure runs smoothly and returns its result, but the second just stops the program and it never wakes up, it doesn't freeze, it's just waiting for the query to execute infinetly.The funny thing is that when I run this program on my machine (Windows 7) everything works fine (takes 4 seconds to execute), but on the server (Windows Server 2008 R2) i get this strange behaviour. I managed to solve this changing Connection Timeout to some other number like 15, but the question is why?
My code:
First proc:

public static bool WorkdayCheck(string sp_name, DateTime CheckDate)
{
    SqlConnection conn = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd = new SqlCommand(sp_name, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 0; //unlimited
    cmd.Parameters.AddWithValue("@checkdate", CheckDate);
    var work = cmd.Parameters.Add("@work", SqlDbType.Bit);
    work.Direction = ParameterDirection.Output;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    if ((bool)cmd.Parameters["@work"].Value) return true;
    else return false;
}

Second proc:

static DataTable GetSql(string sp_name, params string[] vars)
{
    SqlConnection.ClearAllPools(); //added this hoping it'd help - it didn't
    DataTable DT = new DataTable();
    SqlConnection conn = new SqlConnection(ConnectionString);
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand(sp_name, conn);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.CommandTimeout = 0; //unlimited
    da.SelectCommand.Parameters.AddWithValue("@date_b_d", vars[0]);
    da.SelectCommand.Parameters.AddWithValue("@broker_id_s", vars[1]);
    da.SelectCommand.Parameters.AddWithValue("@dogovor_id_s", vars[2]);
    DataSet ds = new DataSet();
    da.Fill(ds, "orders_list");
    DT = ds.Tables["orders_list"];
    return DT;
}

from MSDN:

    You can set the amount of time a connection waits to time out by using the ConnectTimeout or Connection Timeout keywords in the connection string. 
A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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