【问题标题】:Data not writing to MYSQL database数据未写入 MYSQL 数据库
【发布时间】: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


【解决方案1】:

使用未赋值的局部变量'xxx'

意味着编译器不能断定你在给定时间给某个变量一个值。为它分配任何内容,包括 null 将删除该错误消息,因为那时编译器会知道您已经考虑过变量的内容。

【讨论】:

  • 用我认为重要的其余部分更新了代码。
  • MySqlDataReader 阅读器;是在使用变量 reader 之前的唯一代码。但是 reader 需要包含任何内容,它应该用于读取任何内容,您希望值来自数据库吗?
  • 是的,我希望读者检查数据库是否包含用户尝试注册帐户的用户名。
  • string constring = "datasource=localhost;port=3306;username=Admin;password=**********"; 我相信这是与数据库的连接。您能否为我建模,我将如何检查 textbox1.text 是否已经在表中的用户名列中?这就是我真正想要为这个问题做的所有事情。
【解决方案2】:

我可能是错的,因为我不直接通过 .net 工作 MySQL,但在我看到的其他答案中,参数是用“?”处理的,而不是“@”

【讨论】:

    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多