Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362333
  • 博文数量: 94
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 823
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 16:49
文章分类

全部博文(94)

文章存档

2015年(1)

2011年(1)

2010年(3)

2008年(8)

2007年(55)

2006年(26)

我的朋友

分类: 数据库开发技术

2007-09-10 09:43:39

数据库中某一字段的一些记录中含有'(单撇号)字符,在使用SQL查询语句查询时容易报错。例如:PlotInfo表中一些记录的在Plantation字段的数据为 O'NEILL 1 ,在程序中,要查询Plantation字段为 O'NEILL 1(使用变量plantation)的记录,使用SQL语句为:dbComm.CommandText = "select * from PlotInfo where plantation='" + plantation + "'"; (O'NEILL 1使用变量plantation代替),这样在执行这条SQL语句的时候就会报错,因为SQL语句相当于select * from PlotInfo where plantation='O'NEILL 1',这样的SQL语句中多了一个'(单撇号),显然是有语法错误的,不能正常执行。解决方法是修改SQL语句为:dbComm.CommandText = "select * from PlotInfo where plantation='" + plantation.Replace("'","''") + "'"; 即将一个'替换成两个连续的',就可以准确执行,不出差错,至于原因就不细究了。
阅读(1907) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~