我收到“超时已过期。在从池中获取连接之前已经过了超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。” 错误,但找不到问题出在哪里。一点帮助,请。:)
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();
}
}
}