insert语句插入语句中,某字段数据类型为字符型,取得控件text值后,text值中有''存在,插入出错。
求解决办法。
代码:
string art_sql = "INSERT INTO [it].[dbo].[Article]([ArticleTitle],[ArticleContent],[UserId],[ArticleTime],[ArticleTypeId])VALUES('" + txtTitle.Text + "','"+Convert.ToString(txtCKEditor.Text.ToString())+"'," + Convert.ToInt32(lblUserId.Text) + ",'" + Convert.ToDateTime(lblTime.Text) + "'," + Convert.ToInt32(ddlArticleType.SelectedValue) + ")";
如txtCKEditor.Text中带有',无法正常插入。
------解决方案--------------------
你可以用函数,把文本中的单引号,替换成两个单引号,就可以了
------解决方案--------------------
比如,你的语句:
string art_sql = "INSERT INTO [it].[dbo].[Article]([ArticleTitle],[ArticleContent],[UserId],[ArticleTime],[ArticleTypeId])VALUES('" + txtTitle.Text + "','"+Convert.ToString(txtCKEditor.Text.ToString())+"'," + Convert.ToInt32(lblUserId.Text) + ",'" + Convert.ToDateTime(lblTime.Text) + "'," + Convert.ToInt32(ddlArticleType.SelectedValue) + ")";
在字符串上,最好替换一下:
string art_sql = "INSERT INTO [it].[dbo].[Article]([ArticleTitle],[ArticleContent],[UserId],[ArticleTime],[ArticleTypeId])VALUES('" + 替换函数(txtTitle.Text,"'","''") + "','"+Convert.ToString(txtCKEditor.Text.ToString())+"'," + Convert.ToInt32(lblUserId.Text) + ",'" + Convert.ToDateTime(lblTime.Text) + "'," + Convert.ToInt32(ddlArticleType.SelectedValue) + ")";