2010-10-07 154 views
0

'附近使用正確的語法我試圖用我的Asp.NET項目將html頁面插入到MySQL中,但出現錯誤;你的SQL語法有錯誤;檢查與您的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(); 
+0

請用實際數據顯示最終查詢 – 2010-10-07 13:21:17

回答

2

使用MySqlCommand.Parameters.Add添加您的參數。此自動轉義並驗證您的參數。

+0

我收到錯誤。我將我的代碼添加到帖子中,可以告訴我我的錯在哪裏?對不起,我只使用MSSQL,並不知道任何關於asp.net mysql連接。 – Xenon 2010-10-07 15:53:22

+0

解決了我的問題有一個')'額外,我刪除它。比我更改我的'@'爲'?'並感謝所有人解決。 – Xenon 2010-10-07 16:05:05

1

其中一個字符串中包含'字符,提前關閉字符串並創建語法錯誤。
(這是一個SQL Injection vulnerability

要解決此問題,您需要使用參數;有關示例,請參閱您的MySQL類的文檔。

+0

我知道這是因爲'字符,但我問如何解決它? – Xenon 2010-10-07 13:35:47

+0

您需要使用參數。請參閱您的文檔 – SLaks 2010-10-07 13:37:10

相關問題