未处理的类型异常'系统堆栈溢出异常#39;发生在mscorlib.dll中

时间:2014-09-09 06:05:55

标签: sql winforms recursion c#-3.0

class BaseData
{
    public string DbConnection = ConfigurationManager.ConnectionStrings["OLTPPOINTER_Connectionstring"].ConnectionString;
    public string DbConnection_Report = ConfigurationManager.ConnectionStrings["OLTPPOINTER_Connectionstring"].ConnectionString;

    protected SqlConnection OpenConnection()
    {
        **SqlConnection conn = new SqlConnection(DbConnection);**----"StackOverflow Exception was unhandled"

        try
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            return conn;
        }
        catch { throw; }
    }

我使用了递归,它有时没有异常。 在某些情况下,它会抛出异常: -

   SqlConnection conn = new SqlConnection(DbConnection); 
                    or
   SqlDataAdapter adpt = new SqlDataAdapter(cmd);
   adpt.Fill(ds, "DataSet");
   CloseConnection(cmd.Connection);

递归调用使得抛出一个Exception.How来处理它

1 个答案:

答案 0 :(得分:0)

是的,递归意味着嵌套的方法调用。太多嵌套方法调用确实会抛出StackOverflow。你应该避免以递归方式打开多个连接。