【发布时间】:2011-10-13 19:20:11
【问题描述】:
我的页面上有文本区域。在那个区域,我必须添加一些 HTML 代码并将其保存到数据库中。它适用于简单的 html,但是当我从“wikipedia”中选择一些文本并将其粘贴并尝试在需要执行 SQL 查询时保存时,出现以下错误:
Incorrect syntax near 's'.
The identifier that starts with '. Interestingly, old maps show the name as <em>Krakow</em>.</p>
<p>Kragujevac experienced a lot of historical turbulence, ' is too long. Maximum length is 128.
The identifier that starts with '>Paleolithic</a> era. Kragujevac was first mentioned in the medieval period as related to the public square built in a sett' is too long. Maximum length is 128.
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure.
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure.
Unclosed quotation mark after the character string '>Belgrade Pashaluk</a>.</p>'
我正在使用 asp mvc 和 razor 引擎。我不知道也许我需要以某种方式输入 html。我还为 ArticleText 属性添加了这个:
[AllowHtml]
public string ArticleText { get; set; }
这是保存到数据库的代码:
string sql = @"insert into tbl_articles
(Text) values
("'" + article.ArticleText"'"+")";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
【问题讨论】:
-
问题是您的 ArticleText 包含需要转义的单引号(又名撇号)。为什么要在这里构建动态 SQL 而不是使用参数化查询?
-
制作一个 proc,它完全符合您在字符串中的操作,并将 HTML 作为参数传递......就像魔术一样。
标签: c# asp.net sql-server asp.net-mvc razor