簡體   English   中英

Windows Server 2008 R2上的連接超時= 0

[英]Connection Timeout=0 on Windows Server 2008 R2

好的,我有一個用C#編寫的程序,該程序通過一系列存儲過程調用從SQL Server 2008獲取一些信息。 我的連接字符串:

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

{0}和{1}由變量填充。
超時為零,因為信息可能非常龐大,可能需要一些時間才能獲取。
第一個過程可以順利運行並返回結果,但是第二個過程只是停止程序,並且永遠不會喚醒,不會凍結,它只是等待查詢無限執行。有趣的是,當我在我的機器(Windows 7)一切正常(需要4秒鍾執行),但是在服務器(Windows Server 2008 R2)上卻出現了這種奇怪的現象。 我設法將連接超時更改為其他一些數值,例如15,但是問題是為什么?
我的代碼:
第一個過程:

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

第二個過程:

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

從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.

更多信息在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM