C# 超时过期异常;找不到哪个连接保持打开状态,或者是否有其他问题
我超时了。从池中获取连接之前经过的超时时间。发生这种情况的原因可能是所有池连接都在使用中,并且已达到最大池大小。错误,但无法找到问题所在。请帮帮忙C# 超时过期异常;找不到哪个连接保持打开状态,或者是否有其他问题,c#,connection-timeout,C#,Connection Timeout,我超时了。从池中获取连接之前经过的超时时间。发生这种情况的原因可能是所有池连接都在使用中,并且已达到最大池大小。错误,但无法找到问题所在。请帮帮忙 public static void Update(string p, int c) { using (SqlConnection conn = new SqlConnection("ConnectionString")) { SqlCommand cmd = new SqlComman
public static void Update(string p, int c)
{
using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
SqlCommand cmd = new SqlCommand();
SqlDataReader myRdr;
cmd.Connection = conn;
cmd.CommandText = "SOME SQL QUERY @parameter";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@parameter", SqlDbType.NVarChar, 10).Value = p;
conn.Open();
myRdr = cmd.ExecuteReader();
if (myRdr.HasRows)
{
while (myRdr.Read())
{
//do something using myRdr data
}
myRdr.Close();
cmd.Dispose();
foreach (DataRow r in dt.Rows)
{
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "SOME SQL QUERY @parameter";
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.Add("@parameter", SqlDbType.NVarChar, 10).Value = r["SomeData"];
myRdr = cmd1.ExecuteReader();
myRdr.Read();
//do something with myRdr data
myRdr.Close();
cmd1.Dispose();
int a = Convert.ToInt32(r["SomeData"]) - Convert.ToInt32(r["SomeData1"]);
if (a >= 0)
{
//do something
}
else
{
//do something else and runn the Update() again with some other parameters
Update(x, y); //I think here is some problem...
//because when this condition is not met and program
//does not need to run Update() again, it goes OK and I get no error
}
}
}
else
{
myRdr.Close();
cmd.Dispose();
}
}
}
您应该命令将调用移动到外部using块之外的某个位置Updatex,y 使用从内部调用它意味着您正在建立到同一数据库的另一个连接,而不首先释放外部连接。如果你得到了大量的递归调用,那么你很快就会耗尽可用的连接 通常,在处理数据库的部分代码中使用递归调用被认为是一种非常糟糕的做法 如果您确实需要这个递归,那么它仍然应该在外部使用之外完成。我建议将您的x,y缓存在某种集合中,并通过在using块之外遍历该集合来调用Updatex,y,如下所示:
public static void Update(string p, int c)
{
// I'd suggest Dictionary, but don't know whether your strings are unique
var recursionParameters = new List<KeyValuePair<string, int>>();
using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
...
//do something else and runn the Update() again with some other parameters
//Update(x, y); Don't do it here! Instead:
recursionParameters.Add(new KeyValuePair<string, int>(x,y));
...
}
foreach (var kvp in recursionParameters
{
Update(kvp.Key, kvp.Value)
}
}
您应该命令将调用移动到外部using块之外的某个位置Updatex,y 使用从内部调用它意味着您正在建立到同一数据库的另一个连接,而不首先释放外部连接。如果你得到了大量的递归调用,那么你很快就会耗尽可用的连接 通常,在处理数据库的部分代码中使用递归调用被认为是一种非常糟糕的做法 如果您确实需要这个递归,那么它仍然应该在外部使用之外完成。我建议将您的x,y缓存在某种集合中,并通过在using块之外遍历该集合来调用Updatex,y,如下所示:
public static void Update(string p, int c)
{
// I'd suggest Dictionary, but don't know whether your strings are unique
var recursionParameters = new List<KeyValuePair<string, int>>();
using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
...
//do something else and runn the Update() again with some other parameters
//Update(x, y); Don't do it here! Instead:
recursionParameters.Add(new KeyValuePair<string, int>(x,y));
...
}
foreach (var kvp in recursionParameters
{
Update(kvp.Key, kvp.Value)
}
}
发生异常时,读卡器可能无法关闭。用try-finally块包围它,所以当您遇到异常时,您的阅读器将关闭finally语句
try
{
myRdr = cmd.ExecuteReader();
// do some other stuff
}
catch(SqlException)
{
// log the exception or deal with ex.
}
finally
{
myRdr.Close(); // you can be sure it will be closed even on exception
}
发生异常时,读卡器可能无法关闭。用try-finally块包围它,所以当您遇到异常时,您的阅读器将关闭finally语句
try
{
myRdr = cmd.ExecuteReader();
// do some other stuff
}
catch(SqlException)
{
// log the exception or deal with ex.
}
finally
{
myRdr.Close(); // you can be sure it will be closed even on exception
}
我需要在满足特定条件时调用Update。我应该把电话转到哪里?我不知道还有什么简单的方法。但我是个新手要帮忙吗;非常感谢,您为我节省了一些时间,让我可以自己尝试理解语法:谢谢您接受,但我还是会重新考虑使用递归调用。如果你能找到一种方法在你的SQL中包含这个逻辑,它的性能会更好。我试过了,但是中间有太多的计算和其他需要满足的条件,所以在SQL查询中这样做对我来说太复杂了:我需要在满足那个特定条件时调用Update。我应该把电话转到哪里?我不知道还有什么简单的方法。但我是个新手要帮忙吗;非常感谢,您为我节省了一些时间,让我可以自己尝试理解语法:谢谢您接受,但我还是会重新考虑使用递归调用。如果您能找到一种方法将此逻辑包含在SQL中,它的性能会更好。我尝试过,但中间有太多的计算和其他需要满足的条件,因此在SQL querry中执行此操作对我来说太复杂了:将使用try finnaly block对其进行搜索,看看是否有帮助,谢谢:请试用Finnaly block,看看是否有帮助,谢谢: