StackOverFlowException被调用

时间:2017-09-22 14:51:35

标签: c# sql visual-studio

这是我在DBConnectionClass中设置对象的代码

    public string ConnectionString { get; set; }
    public Schools  Schools  = new Schools();
    public Branches Branches = new Branches();
    public Students Students = new Students();

这是DBConnection类的构造函数:

      public DbConnectionClass()
      {
        ConnectionString = "Data Source=LRO-SH-32;Initial Catalog=DatabaseDB;Integrated Security=True";
        Con = new SqlConnection(ConnectionString);
        Con.Open();
      }

以下是一些查询变量:

    public string InsertAllQuery = " INSERT INTO Schools ( SchoolName, SchoolLocation, SchoolPhoneNo, SchoolEmailId) VALUES (@SchoolId, @SchoolName, @SchoolLocation, @SchoolPhoneNo, @SchoolEmailId) ";
    public string UpdateAllQuery = " UPDTAE Schools SET (SchoolName=@SchoolName, SchoolLocation=@SchoolLocation, SchoolPhonNo=@SchoolPhoneNo, SchoolEmailId=@SchoolEmailId) WHERE Id=@Id ";
    public string DeleteAllQuery = " DELETE FROM Schools WHERE Id=@Id ";
    public string SelectAllQuery = " SELECT * FROM Schools WHERE Id=@Id";
    // DbConnectionClass newDbInstance = new DbConnectionClass();
    public string SchoolsInsertQuery {
        get { return InsertAllQuery; }
        set { InsertAllQuery = value; }
    }

这是我的最终查询:

   public string InsertQuery
    {
        get { return InsertAllQuery; }
        set { InsertAllQuery = Schools.SchoolsInsertQuery + Branches.BranchInsertQuery + Students.StudentsInsertQuery ; }

    }

这是完整的类代码:

       public class DbConnectionClass
{
//Connection String


    public string ConnectionString { get; set; }
    public Schools  Schools  = new Schools();
    public Branches Branches = new Branches();
    public Students Students = new Students();

    //Queries 

    public string InsertAllQuery { get; set; }
    public string UpdateAllQuery { get; set; }
    public string DeleteAllQuery { get; set; }
    public string SelectAllQuery { get; set; }
    public string InsertQuery
    {
        get { return InsertAllQuery; }
        set { InsertAllQuery = Schools.SchoolsInsertQuery + Branches.BranchInsertQuery + Students.StudentsInsertQuery ; }

    }



//Starting Database connection definition
    public SqlConnection Con;
    public DbConnectionClass()
    {
        ConnectionString = "Data Source=LRO-SH-32;Initial Catalog=DatabaseDB;Integrated Security=True";
        Con = new SqlConnection(ConnectionString);
        Con.Open();
    }

    public void SaveData()
    {
        SqlCommand cmd = new SqlCommand(InsertQuery, Con);
        cmd.ExecuteNonQuery();
    }


    public void DbConnectionClose()
    {
        Con.Close();
    }

}

} 现在问题是,它在上面代码的第2行第1块给出了Stackoverflow异常。你能告诉我发生了什么以及我们应该如何处理它: 这是调用堆栈>

         >  SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 15   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.Students.Students() Line 27 C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP.DbConnectionClass.DbConnectionClass() Line 17   C#
SchoolManagementSystemOOP.dll!SchoolManagementSystemOOP._Default._Default() Line 16 C#
[External Code] 

这是我的学生班级代码

         public class Students 
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }

    public string LastName { get; set; }
    public string PhoneNo { get; set; }
    public string EmailId { get; set; }
    public string InsertAllQuery { get; set; }
    public string UpdateAllQuery { get; set; }

    public string DeleteAllQuery { get; set; }

    public string SelectAllQuery {get;set;}

    DbConnectionClass newDbInstance = new DbConnectionClass();

    public string StudentsInsertQuery
    {
        get { return InsertAllQuery; }
        set
        {
           InsertAllQuery = " INSERT INTO AddStudents (FirstName, LastName, PhoneNo, EmailId) VALUES (@FirstName, @LastName, @PhoneNo, @EmailId) ";
        }
    }
    //Constructors
    public Students()
    {
    }



}

2 个答案:

答案 0 :(得分:0)

DbConnectionClass实例化一个Schools对象,该对象看起来然后实例化DbConnectionClass对象,创建一个无限循环

答案 1 :(得分:0)

根据您的堆栈跟踪,Students.Students()正在实例化DbConnectionClass()的新实例,该实例会创建Students的新实例,依此类推