【发布时间】:2013-12-08 22:52:04
【问题描述】:
我在将信息写入数据库时遇到问题。我正在使用从观看 Youtube 上的视频中学到的方法,我们制作了 query string 并输入了所有 INSERT INTO 的东西,但我正在尝试一种新的方法,使用我读过的参数来更安全并防止我读过的 SQL 注入很糟糕。
我正在使用下面的代码,一切运行正常,没有错误,它正在连接到我的 FTP 服务器并且一切正常。我的问题是它没有像以前使用安全性较低的代码时那样将任何输入的用户信息写入 MYSQL 数据库。
另外,如果回答这个问题的人知道如何检查用户名是否已被数据库收录,请告诉我,这样我就不必单独提出问题了。我试过类似的东西
if(Reader.HasRows) MessageBox.Show("Username Taken");
但我收到了错误Use of unassigned local variable 'reader'。
我正在使用的代码:
private void btnRegister_Click(object sender, EventArgs e)
{
MySqlDataReader reader;
int numerror = 0;
if (RUsernameTextBox.Text == "")
{
numerror = numerror + 1;
}
if (RPasswordTextBox.Text == "")
{
numerror = numerror + 1;
}
if (REmailTextBox.Text == "")
{
numerror = numerror + 1;
}
if (numerror == 1)
{
ErrorLabel.Text = "*1 required field is blank.";
}
else if (numerror == 2)
{
ErrorLabel.Text = "*2 required fields are blank";
}
else if (numerror == 3)
{
ErrorLabel.Text = "*3 required fields are blank";
}
else
{
if (reader.HasRows) CMessageBox("Error", "");
//add the user to the MySQL database
string HashedPassword = EncodePassword(RPasswordTextBox.Text);
string constring = "datasource=localhost;port=3306;username=Admin;password=**********";
using (MySqlConnection con = new MySqlConnection(constring))
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO userinfo.users (username,password,email,premium,picture) VALUES (@username, @hashedpassword, @email , @premium , @picture);");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
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
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MakeLoginVisible();
try
{
string TempFolder = Path.GetTempPath();
RProfilePicture.Image.Save(@"C:\temp\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", System.Drawing.Imaging.ImageFormat.Png);
ftp 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());
}
}
catch (Exception ex)
{
CMessageBox("Error", ex.Message.ToString());
}
}
}
【问题讨论】:
-
那你能告诉我们你再次运行代码时遇到了什么奇怪的错误吗? “我遇到了一些我似乎不记得的奇怪错误”还不够好。
-
I got some weird error that I can't seem to remember= 没用 -
@AndersLindén 很抱歉没有弄清楚,我在调试时遇到的错误是
Use of unassigned local variable 'reader' -
你遗漏了很多代码,所以我看不到 if(Reader.HasRows) MessageBox.Show("Username Taken");前面有
标签: c# mysql sql .net database