字符串后面的未闭合引号 [英] Unclosed quotation mark after the character string

查看:134
本文介绍了字符串后面的未闭合引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在传递一些这样的查询:

   select * from Item where Item_Name比如' + GridViewItem.Rows [ 0 ]。单元格[ 1 ]。ToString( )+  '; 



项目名字包含单引号,所以当我通过时,上面的错误即将来临。

建议我一些答案......

谢谢...

解决方案

您应该使用参数查询而不是字符串连接。



a)它解决了包含引号的字符串的问题。

b)有助于保护您的查询免受SQL注入攻击。



这个站点和谷歌上有很多资源解释如何创建一个paramterized查询。


使用类似的东西:

 GridViewItem.Rows [< span class =code-digit> 0 ]。单元格[ 1 ]。ToString()。替换( ' '' 



或者

 GridViewItem.Rows [ 0 ]。单元格[ 1 ]。ToString()。替换( '  \'





由于有很多sql语言,我不太确定,但你可以轻松尝试其中一种,看看会发生什么/ BLOCKQUOTE>

I am passing some query like this:

"select * from Item where Item_Name like '" + GridViewItem.Rows[0].Cells[1].ToString() + "'";


Item Name contains a single quote, so when I am passing the above error is coming.
Suggest me some answers...
Thank You...

解决方案

You should be using Parameter queries instead of string concatenation.

a) it solves the issue with strings having quotes in them.
b) helps secure your queries against SQL Injection attacks.

There are alot of resources on this site and on google explaining how to create a paramterized query.


use something like:

GridViewItem.Rows[0].Cells[1].ToString().Replace("'","''") 


or maybe

GridViewItem.Rows[0].Cells[1].ToString().Replace("'","\'")



I'm not really sure because of the many sql languages, but you can easily try one of these and see what happens


这篇关于字符串后面的未闭合引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆