简体   繁体   English

从 C# 对象创建数据读取器

[英]Creating a Data Reader from a C# Object

I am having trouble with the following line:我在使用以下行时遇到问题:

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

It keeps returning the following error:它不断返回以下错误:

System.InvalidOperationException: 'Connection must be valid and open.' System.InvalidOperationException: '连接必须有效且打开。'

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();
        }

I have tested the connection, it does work just fine, and it works during my login process.我已经测试了连接,它工作得很好,并且在我的登录过程中工作。

The only other thing is this whole process utilises my clsDatabase class, that is where MySqlDataReader DataReader = objDatabase.SetCommandType("GetRaceLevels").ExecuteReader();唯一的另一件事是整个过程利用了我的 clsDatabase 类,即MySqlDataReader DataReader = objDatabase.SetCommandType("GetRaceLevels").ExecuteReader(); comes from.来自。

This is the function on the clsDatabse class:这是 clsDatabse 类上的函数:

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;
        }

I hope this all makes sense and I am not being extremely thick.我希望这一切都是有道理的,而且我不是特别厚。 Any help would be much appreciated!任何帮助将非常感激! Thank you!谢谢!

EDIT: The 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;
        }
    }

Everytime you call GetConnection() you're getting a brand new one.每次您调用GetConnection()您都会得到一个全新的。 A new connection starts closed.一个新的连接开始关闭。 Now, look at your SetCommandType method.现在,看看您的SetCommandType方法。 It's instancianting a new MySqlCommand with a brand new connection, which is closed.它正在使用一个全新的连接实例化一个新的 MySqlCommand,该连接已关闭。 You could open the connection in the method, but that is error prone, as you would end up with no means to close the connection afterward.您可以在该方法中打开连接,但这很容易出错,因为之后您将无法关闭连接。 Instead, instantiate the connection where you want to use it.相反,在您想要使用它的地方实例化连接。 Also, use using statements for better IDisposable handling.此外,使用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!");
            }
        }               
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM