从 C# 对象创建数据 Reader

Creating a Data Reader from a C# Object

我在使用以下行时遇到问题:

MySqlDataReader DataReader = objDatabase.SetCommandType("GetRaceLevels").ExecuteReader();

它不断返回以下错误:

System.InvalidOperationException: 'Connection must be valid and open.'

private void RegisterForm_Load(object sender, EventArgs e)
        {

            HideMe();
            MoveMe(-180);

            MySqlConnection myConnection = objDatabase.GetConnection(); //must save the object based connection to a local variable for some reason.

            myConnection.Open();
            MySqlDataReader DataReader = objDatabase.SetCommandType("GetRaceLevels").ExecuteReader();

            try
            {
                while (DataReader.Read())
                {
                    cboRunnerTypes.Items.Add(DataReader[1]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Critical error!");
            }
            myConnection.Close();
        }

我已经测试了连接,它工作正常,并且在我的登录过程中工作。

唯一的另一件事是整个过程利用了我的 clsDatabase class,这就是 MySqlDataReader DataReader = objDatabase.SetCommandType("GetRaceLevels").ExecuteReader(); 的来源。

这是 clsDatabse 上的函数 class:

public MySqlCommand SetCommandType(string sProcedureName)
        {
            MySqlCommand myCommand = new MySqlCommand(sProcedureName, GetConnection()); //I think the problem is here, how am I fixing it though?
            myCommand.CommandType = CommandType.StoredProcedure;
            return myCommand;
        }

我希望这一切都是有道理的,我不是太厚。任何帮助将非常感激!谢谢!

编辑:class:

class clsDatabase
    {
        private const string conString = "server = ; database = ; user = ; password = ; charset = utf8";
        public MySqlConnection GetConnection()
        {
            MySqlConnection myConnection = new MySqlConnection(conString);
            return myConnection;
        }

        public void EmailCommandCaller(MySqlCommand myCommand, string sEmail, string sContent)
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue(sEmail, sContent);
        }

        public void LoginCommandCaller(MySqlCommand myCommand, string sEmail, string sPassword, string sEmailContent, string sPasswordConetnt)
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue(sEmail, sEmailContent);
            myCommand.Parameters.AddWithValue(sPassword, sPasswordConetnt);
        }
        public MySqlCommand SetCommandType(string sProcedureName)
        {
            MySqlCommand myCommand = new MySqlCommand(sProcedureName, GetConnection()); //I think the problem is here, how am I fixing it though?
            myCommand.CommandType = CommandType.StoredProcedure;
            return myCommand;
        }
    }

每次您致电 GetConnection(),您都会得到一个全新的。一个新的连接开始关闭。现在,看看您的 SetCommandType 方法。它正在实例化一个带有全新连接的新 MySqlCommand,该连接已关闭。您可以在该方法中打开连接,但这很容易出错,因为之后您将无法关闭连接。相反,在您要使用它的地方实例化连接。此外,使用 using 语句以获得更好的 IDisposable 处理。

private void RegisterForm_Load(object sender, EventArgs e)
{
    HideMe();
    MoveMe(-180);
    using(MySqlConnection myConnection = new MySqlConnection(conString)){
        myConnection.Open();
        MySqlCommand myCommand = new MySqlCommand("GetRaceLevels", myConnection); 
        myCommand.CommandType = CommandType.StoredProcedure;
        using(MySqlDataReader DataReader = myCommand.ExecuteReader()){
            try
            {
                while (DataReader.Read())
                {
                    cboRunnerTypes.Items.Add(DataReader[1]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Critical error!");
            }
        }               
    }
}