当我尝试使用相同的连接向数据库添加数据或关闭或处理读取器并使程序冻结时,我遇到了问题。 如果您不明白我想解释的内容,请提出问题。我对整个事情感到有点困惑,所以我可能会有意义。
代码:
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());
}
}
}
}
}
}
答案 0 :(得分:0)
在这种情况下,你真的不需要读者。您可以在查询中获取计数并使用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
答案 1 :(得分:0)
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());
}
}