MySqlReader在错误的地方?

时间:2014-03-26 07:33:07

标签: c# mysql

我有这段代码检查数据库中已有的用户信息,如果数据相同则更新信息:

// Connection to the database.
DbConnect();

// If the Connection is sound, checks the Username and Password are the same as the input.
try
{
    mySqlCommand = mySqlConnect.CreateCommand();
    mySqlCommand.CommandText = Database_MySQLDef.SELECTFROMUSERS + "UserName = '" + UModel.Name + "' AND UserPassword = '" + UModel.Pwd + "';";
    mySqlReader = mySqlCommand.ExecuteReader();

    while (mySqlReader.Read())
    {
        // If the query could not be found, outputs an error.
        if (UModel.Pwd != Convert.ToString(mySqlReader["UserPassword"]))
        {
            Console.WriteLine("Query does not match");
        }
        // If the query is found, changes the input of the password.
        else
        {
            try
            {
                // Updates the UserPassword in the Db.
                mySqlCommand = mySqlConnect.CreateCommand();
                mySqlCommand.CommandText = Database_MySQLDef.UPDATEUSERS + "UserPassword = '" + UModel.ConfirmPwd + "' WHERE UserName = '" + UModel.Name + "';";
                mySqlCommand.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
            }
            // Closes the Reader and empties the Command object.
            mySqlReader.Close();
            mySqlCommand.ExecuteNonQuery();
        }
    }
}
catch (MySqlException e)
{
    Console.WriteLine(e.Message);
}
// When the query is finished, terminates the connection to the Database.
finally
{
    if (mySqlConnect != null)
    {
        mySqlConnect.Close();
    }
}

注意几个月前我刚刚开始写这篇文章,所以我知道这很难看。然而,目前的主要问题是MySqlReader.Close()的位置似乎在错误的位置。它现在的位置(在try/catch语句中的else块之后)

catch (MySqlException e)
{
    Console.WriteLine(e.Message);
}
// Closes the Reader and empties the Command object.
mySqlReader.Close();
mySqlCommand.ExecuteNonQuery();

它会返回错误:

  

已经有一个与此Connection关联的打开DataReader,必须先关闭它。

我最初之前 try/catch声明中的else

else
{
    try
    {
        // Closes the Reader and empties the Command object.
        mySqlReader.Close();
        mySqlCommand.ExecuteNonQuery();

给出了错误:

  

阅读器关闭时无效尝试阅读。

有人可以指出出了什么问题以及我可以做些什么来解决它?

1 个答案:

答案 0 :(得分:1)

您应该从while循环而不是else中断。在循环之后,您应该关闭阅读器并重新创建mySqlCommand。像这样:

{
    bool passwordFounded = false;
    while (mySqlReader.Read())
    {
        if (UModel.Pwd != Convert.ToString(mySqlReader["UserPassword"]))
        {
            Console.WriteLine("Query does not match");
        }
        else
        {
            passwordFounded = true;
            break;
        }
    }

    // Closes the Reader and empties the Command object.
    mySqlReader.Close();

    if (passwordFounded)
    {
        try
        {
            // Updates the UserPassword in the Db.
            mySqlCommand = mySqlConnect.CreateCommand();
            mySqlCommand.CommandText = Database_MySQLDef.UPDATEUSERS + "UserPassword = '" + UModel.ConfirmPwd + "' WHERE UserName = '" + UModel.Name + "';";
            mySqlCommand.ExecuteNonQuery();
        }
        catch (MySqlException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}