【问题标题】:MySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax (c#)MySQL 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法 (c#)
【发布时间】:2016-05-22 17:46:33
【问题描述】:
 private void searchButton_Click(object sender, EventArgs e)
    {
        string constring = "datasource=localhost;port=3306;Initial Catalog = 'dbcpu'; username = root; password =";
        string query = "select * from dbcpu.student_profile where name'" + maskedTextBox1.Text + "'";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string Lname = myReader.GetString(myReader.GetOrdinal("Lname"));
                textBoxLname.Text = Lname;

                byte[] imgg = (byte[])(myReader["Pic"]);
                if (imgg == null)
                    pictureBox1.Image = null;
                else
                {
                    MemoryStream mstream = new MemoryStream(imgg);
                    pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

【问题讨论】:

  • 也许写点东西是个好主意,而不仅仅是粘贴一些随机的代码?
  • 您缺少 = 符号 WHERE Name =.. 更好的是,使用 Parameters.Add 方法
  • 您真的不想在用户输入上使用字符串连接来构建查询。谷歌“SQL 注入攻击”很好地解释了原因。这是目前最糟糕的安全反模式之一。
  • 已经修复。谢谢顺便说一句。我只是错过了一些要输入的字符。 :D 我只是困了。哈哈。 :D

标签: c# mysql syntax


【解决方案1】:

如果我们要输出 SQL 文本的内容,看起来我们会得到这样的东西;

  ... where name'foo'

这看起来不像是有效 SQL 语句的一部分。也许你打算这样?

  ... where name = 'foo'
                ^^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2022-01-14
    • 2019-03-25
    • 2015-10-23
    相关资源
    最近更新 更多