c#中的单引号

1 篇文章 0 订阅

字符串加入''单引号:代码分析器遇到一个单引号,就认为字符串文字开始,然后遇到下一个单引号的时候,它不会立即认为字符串结束,而是再读取下一个字符,如果还是单引号,则把这两个单引号解释为字符串中的一个单引号,并且继续分析后面的字符,直到遇到一个单引号后不是单引号,就认为这个字符串结束。

例一:
string.Format("delete from [escale_std] where  identifier= "+"'dataGridView1.Rows[1].Cells[0].Value.ToString().Trim()'")
返回的结果
delete from [escale_std] where  identifier= 'dataGridView1.Rows[1].Cells[0].Value.ToString().Trim()'  //没有变换数值,失败

例二:
string.Format("delete from [escale_std] where  identifier= "+dataGridView1.Rows[1].Cells[0].Value.ToString().Trim())
返回的结果
delete from [escale_std] where  identifier= F0779  //F0779无单引号,失败

例三:
string.Format("delete from [escale_std] where  identifier= "+"'"+dataGridView1.Rows[1].Cells[0].Value.ToString().Trim()+"'")
返回的结果
delete from [escale_std] where  identifier= 'F0779'  //有单引号,符合预期,其中+"'" 是为了增加前后单引号'

例四:
string.Format("delete from [escale_std] where  identifier= '"+dataGridView1.Rows[1].Cells[0].Value.ToString().Trim()+"'")
返回的结果
delete from [escale_std] where  identifier= 'F0779'  //有单引号,符合预期

例五:
string.Format("delete from [escale_std] where  identifier= '{0}'",dataGridView1.Rows[1].Cells[0].Value.ToString().Trim())
返回的结果
delete from [escale_std] where  identifier= 'F0779'  //符合预期,有更好的可读性

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值