【问题标题】:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 ' 附近使用的正确语法
【发布时间】:2010-10-07 13:19:28
【问题描述】:

我正在尝试使用我的 Asp.NET 项目将 html 页面插入 MySQL,但出现错误;

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'de Osman patlaması', '', '<div style=\"text-align: center\">\r\n<img src=\"/i' at line 1

如何解决我的服务器端代码存在的问题;

MySqlConnection myCon = new MySqlConnection();
myCon.ConnectionString = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;

string query = @"INSERT INTO `test`.`posts` (`id`, `author`, `title`, `description`, `content`, `ispublished`, `iscommentsenabled`, `pubDate`, `lastModified`, `raters`, `rating`, `slug`, `tags`, `categories`) VALUES (NULL, '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}')";
query = String.Format(query, p.author, p.title, p.description, p.content, p.ispublished, p.iscommentsenabled, p.pubDate, p.lastModified, p.raters, p.rating, p.slug, p.tags, p.categories);

cmd.CommandText = query;
cmd.Connection = myCon;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

感谢您的帮助。

    MySqlConnection myCon = new MySqlConnection();
    myCon.ConnectionString = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
    MySqlCommand cmd = new MySqlCommand(@"INSERT INTO posts (id,  author ,  title ,  description ,  content ,  ispublished ,  iscommentsenabled ,  pubDate 
    ,  lastModified ,  raters ,  rating ,  slug ,  tags ,  categories ) 
        VALUES (@id ,@author ,@title ,@description ,@content ,@ispublished ,@iscommentsenabled ,@pubDate ,@lastModified ,@raters ,@rating ,@slug ,@tags ,
        @categories ))", myCon);
    cmd.CommandType = CommandType.Text;

    cmd.Parameters.AddWithValue("@id", null);
    cmd.Parameters.AddWithValue("@author", p.author);
    cmd.Parameters.AddWithValue("@title", p.title);
    cmd.Parameters.AddWithValue("@description", p.description);
    cmd.Parameters.AddWithValue("@content", p.content);
    cmd.Parameters.AddWithValue("@ispublished", p.ispublished);
    cmd.Parameters.AddWithValue("@iscommentsenabled", p.iscommentsenabled);
    cmd.Parameters.AddWithValue("@pubDate", p.pubDate);
    cmd.Parameters.AddWithValue("@lastModified", p.lastModified);
    cmd.Parameters.AddWithValue("@raters", p.raters);
    cmd.Parameters.AddWithValue("@rating", p.rating);
    cmd.Parameters.AddWithValue("@slug", p.slug);
    cmd.Parameters.AddWithValue("@tags", p.tags);
    cmd.Parameters.AddWithValue("@categories", p.categories);
    myCon.Open();
    cmd.Prepare();
    cmd.ExecuteNonQuery();
    myCon.Close();

【问题讨论】:

  • 请显示最终查询,其中包含实际数据

标签: asp.net mysql


【解决方案1】:

使用MySqlCommand.Parameters.Add 添加您的参数。此自动转义并验证您的参数。

【讨论】:

  • 我遇到了错误。我添加了我的代码来发布你能说我的错在哪里吗?抱歉,我只使用了 MSSQL,对 asp.net mysql 连接一无所知。
  • 解决了我的问题有一个 ')' 额外,我删除了它。比我把我的“@”改成“?”已经解决了谢谢大家。
【解决方案2】:

您的一个字符串中有一个' 字符,提前关闭字符串并产生语法错误。
(这是SQL Injection vulnerability

要解决这个问题,您需要使用参数;有关示例,请参阅 MySQL 类的文档。

【讨论】:

  • 我知道这是因为 ' 字符,但我问如何解决它?
  • 你需要使用参数。查阅您的文档
猜你喜欢
  • 2011-08-07
  • 2012-05-02
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多