0

我有以下简单的类来管理我的 SQL 数据库操作

public class DatabaseManager
    {


        private string CommandString
        {
             set { CommandString = GetCommandString(commandtype); }
             get { return CommandString; }
        }
        public string commandtype
        {
            set;
            get;
        }



        public DatabaseManager(string commandtype)
        {
            commandtype = this.commandtype;
            CommandString = GetCommandString(commandtype);
        }

        public DatabaseManager()
        {

        }       


        public static SqlConnection CreateConnection()
        {
            return new SqlConnection(Properties.Settings.Default.connectionString);
        }




        //returns a datatable if the command requires a dataadapter
        public DataTable ExecuteSelect()
        {
            var x = new DataTable();
            using (var da = new SqlDataAdapter(CommandString, DatabaseManager.CreateConnection()))
                {
                    da.Fill(x);
                }

            return x;
        }




        private string GetCommandString(string commandtype)
        {


            switch (commandtype)
            {
                // select commands
                case ("SELECTMARGINS"): CommandString = "select * from margins"; break;
                case ("SELECTRANKS"): CommandString = "select * from ranks"; break;
                /...and other commands

            return CommandString;
        }

    }

我得到一个 Stackoverflow 异常get { return CommandString; }

4

4 回答 4

5

您不能让属性返回本身(它会创建一个无限循环)。

    private string _CommandString;
    public string CommandString
    {
         set { _CommandString = GetCommandString(commandtype); }
         get { return _CommandString; }
    }
于 2013-03-07T18:51:27.200 回答
5

功能是你的get问题

 get { return CommandString; }

这相当于以下的士气

public string GetCommandString() { 
  return GetCommandString();
}

这只会创建无限递归,最终StackOverflowException会抛出 a 。您需要更改getandset以对包含实际值的支持字段进行操作并使用它来代替

private string _commandString;
public string CommandString {
  get { return _commandString; }
  set { _commandString = GetCommandString(commandtype); }
}
于 2013-03-07T18:53:21.593 回答
1

您无法设置甚至获取CommandString,在这种情况下您必须创建一个私有变量。

private string _commandString;
public string CommandString
{
     set { _commandString = GetCommandString(commandtype); }
     get { return _commandString; }
}

您当前的代码中发生的事情是您正在执行以下操作:

CommandString = "x";

调用

CommandString = GetCommandString(type);

调用

CommandString = GetCommandString(type);

等等......所以它一直循环直到它溢出。私有变量可防止您一遍又一遍地设置相同的属性

此外,您似乎从未真正使用value传递给 set 函数,这似乎是一个错误

于 2013-03-07T18:51:22.887 回答
1

您不能让 Get 函数返回自身,它只会导致它无限尝试检索自身,直到堆栈溢出。

创建一个私有变量来获取并设置为:

private string _CommandString;
private string CommandString
{
    //Also you probably want to change commandtype to value, since you will be
    //discarding whatever you attempt to set the variable as
    set { _CommandString = GetCommandString(commandtype); } 
    get { return _CommandString; }
}
于 2013-03-07T18:51:33.540 回答