我需要通过 C# 将文本插入到 MySQL 表中。dbConnect
是我自己创建的适配器,ProcessNonQuery
只需要一个字符串。
我无法插入包含字符"
和'
.
这是我的尝试:
public void InsertArticle(string name, string title, string keywords, string desc, string content, int idCategory,bool featured)// int level, int typeArt
{
this.dbConnect.ProcessNonQuery(" set global sql_mode=\"NO_BACKSLASH_ESCAPES,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\"");
string strFeatured;
if (featured)
strFeatured = "1";
else
strFeatured = "0";
content = content.Replace("'", "\'");
StringBuilder sbInsertArticle = new StringBuilder("INSERT INTO Article(NameArt, TitleArt, keywordsArt, DescArt, ContentArt, idCategory, idLevel, idTypeArt, Featured) VALUES('");
sbInsertArticle.Append(name); sbInsertArticle.Append("', '");
sbInsertArticle.Append(title); sbInsertArticle.Append("', '");
sbInsertArticle.Append(keywords); sbInsertArticle.Append("', '");
sbInsertArticle.Append(desc); sbInsertArticle.Append("', '");
sbInsertArticle.Append(content); sbInsertArticle.Append("', '");
sbInsertArticle.Append(idCategory.ToString()); sbInsertArticle.Append("', '");
sbInsertArticle.Append(1); sbInsertArticle.Append("', '");
sbInsertArticle.Append(1); sbInsertArticle.Append("', '");
sbInsertArticle.Append(strFeatured); sbInsertArticle.Append("')");
string strInsertArticle = sbInsertArticle.ToString();
this.dbConnect.ProcessNonQuery(strInsertArticle);
}