好的,我有一个用C#编写的程序,它通过一系列存储过程调用从SQL Server2008获取一些信息。我的连接字符串:
Data Source={0};Initial Catalog={1}; Integrated Security=True; Connection Timeout=0{0}和{1}由变量填充。
超时为零,因为信息可能非常庞大,可能需要一些时间才能获取。
第一个程序运行顺利并返回结果,但第二个只是停止程序,它永远不会醒来,它不会冻结,它只是等待查询执行infinetly.The有趣的是,当我在我的机器(Windows7)上运行这个程序时,一切都正常(需要4秒执行),但在服务器(Windows Server2008 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;
}发布于 2014-04-28 18:45:33
来自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.更多信息here
https://stackoverflow.com/questions/23338879
复制相似问题