【问题标题】:Everything freezes when closing or disposing a reader关闭或处置阅读器时,一切都会冻结
【发布时间】:2013-12-09 03:45:09
【问题描述】:

当我尝试使用相同的连接将数据添加到数据库或关闭或处置阅读器并让程序冻结时,我遇到了阅读器打开的问题。 如果您不明白我要解释的内容,请提出问题。我对整个事情有点困惑,所以我可能理解为 0。

代码:

private void btnRegister_Click(object sender, EventArgs e)
{
    int numerror = 0;
    if (RUsernameTextBox.Text == "")
    {
        numerror++;
    }

    if (RPasswordTextBox.Text == "")
    {
        numerror++;
    }

    if (REmailTextBox.Text == "")
    {
        numerror++;
    }

    if (numerror > 0)
    {
        ErrorLabel.Text = "*" + numerror + " required field" + (numerror != 1 ? "s are" : " is") + " blank.";
    }
    else
    {
        var constring = "datasource=localhost;port=3306;username=Admin;password=**********;";
        using (var con = new MySqlConnection(constring))
        {
            con.Open();

            var cmd0 = new MySqlCommand("select username from userinfo.users where username=@username");
            cmd0.CommandType = CommandType.Text;
            cmd0.Connection = con;
            cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
            using (var reader = cmd0.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    CMessageBox("Error", "Username is already in use.");
                    reader.Close();
                }
                else
                {
                    reader.Close();
                    var HashedPassword = EncodePassword(RPasswordTextBox.Text);
                    var cmd = new MySqlCommand("INSERT INTO userinfo.users (username,password,email,premium,picture) VALUES (@username, @hashedpassword, @email , @premium , @picture);");
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Username", RUsernameTextBox.Text);
                    cmd.Parameters.AddWithValue("@hashedpassword", HashedPassword);
                    cmd.Parameters.AddWithValue("@email", REmailTextBox.Text);
                    cmd.Parameters.AddWithValue("@premium", "0");
                    cmd.Parameters.AddWithValue("@picture", "ftp://***.***.*.**/Profile Pictures/" + RUsernameTextBox.Text + "/" +  Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");

                    try
                    {
                        cmd.ExecuteNonQuery();
                        MakeLoginVisible();

                        var TempFolder = Path.GetTempPath();
                        RProfilePicture.Image.Save("C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", System.Drawing.Imaging.ImageFormat.Png);
                        var ftpclient = new ftp("ftp://***.***.*.**/", "Admin", "**********");

                        ftpclient.createDirectory("Profile Pictures/" + RUsernameTextBox.Text);
                        ftpclient.upload("Profile Pictures/" + RUsernameTextBox.Text + "/" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", "C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");
                        MakeLoginVisible();
                        CMessageBox("Success!", "AirSpace Account '" + RUsernameTextBox.Text + "' Created.");
                        ftpclient = null;
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        CMessageBox("Error", ex.Message.ToString());
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# mysql winforms freeze sqldatareader


    【解决方案1】:
    using (var con = new MySqlConnection(constring))
    using(var cmd0 = new MySqlCommand("Select count(*) from userinfo.users where username=@username", con))
    {
        con.Open();
        cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
        if((int)cmd0.ExecuteScalar() > 0)
        {
            CMessageBox("Error", "Username is already in use.");
            return;
        }
    }
    
    using (var con = new MySqlConnection(constring))
    using(var cmd= new MySqlCommand("INSERT INTO userinfo.users (username,password,email,premium,picture) VALUES (@username, @hashedpassword, @email , @premium , @picture)", con))
    {
        con.Open();
        var HashedPassword = EncodePassword(RPasswordTextBox.Text);
        cmd.Parameters.AddWithValue("@Username", RUsernameTextBox.Text);
        cmd.Parameters.AddWithValue("@hashedpassword", HashedPassword);
        cmd.Parameters.AddWithValue("@email", REmailTextBox.Text);
        cmd.Parameters.AddWithValue("@premium", "0");
        cmd.Parameters.AddWithValue("@picture", "ftp://***.***.*.**/Profile Pictures/" + RUsernameTextBox.Text + "/" +  Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");
    
        try
        {
            cmd.ExecuteNonQuery();
            MakeLoginVisible();
    
            var TempFolder = Path.GetTempPath();
            RProfilePicture.Image.Save("C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", System.Drawing.Imaging.ImageFormat.Png);
            var ftpclient = new ftp("ftp://***.***.*.**/", "Admin", "**********");
    
            ftpclient.createDirectory("Profile Pictures/" + RUsernameTextBox.Text);
            ftpclient.upload("Profile Pictures/" + RUsernameTextBox.Text + "/" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", "C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");
            MakeLoginVisible();
            CMessageBox("Success!", "AirSpace Account '" + RUsernameTextBox.Text + "' Created.");
            ftpclient = null;
        }
        catch (Exception ex)
        {
            CMessageBox("Error", ex.Message.ToString());
        }
    }
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您实际上并不需要阅读器。您可以在查询中获取计数并使用ExecuteScalar()

      var cmd0 = new MySqlCommand("select count(*) from userinfo.users where username=@username");
      cmd0.CommandType = CommandType.Text;
      cmd0.Connection = con;
      cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
      if (((int)cmd0.ExecuteScalar()) > 0)
          CMessageBox("Error", "Username is already in use.");
      else
         ... the rest of the code
      

      【讨论】:

      • 使用该代码,我得到`Specified cast is not valid.` for if ((int)cmd0.ExecuteScalar() == 0)
      • 添加了一组括号。对此感到抱歉。另外,请确保将查询更改为计数。
      • 并将 == 更改为 > 否则它会反过来比较它。
      • 我仍然收到“指定的演员表无效”:(
      • 调试时cmd0.ExecuteScalar()返回什么?
      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 2018-11-09
      • 1970-01-01
      相关资源
      最近更新 更多