例子:假设text.text的内容存在撇号" ' "
         strsql="select * from table where id='" + text.text + "'";
         SqlDataAdapter da=new SqlDataAdapter(strsql,dbcon);
DataSet ds=new DataSet();
da.Fill(ds,dstable);我要用到一个DataSet对象,要用SqlDataReader对象来填充~但是由于sql语句strsql存在撇号会导致错误~有无没有办法好似command对象那样用Parameters.Add增加参数来解决的方法?

解决方案 »

  1.   

    strsql="select * from table where id='" + text.text.Replace("'","''") + "'";
             SqlDataAdapter da=new SqlDataAdapter(strsql,dbcon);
    DataSet ds=new DataSet();
    da.Fill(ds,dstable);
      

  2.   

    说明,转义字符,两个连续的单引号在SQL语句里代表一个单引号(字符)
      

  3.   

    高手我试试了用查询分析器输入select * from table where id =''a''
    它会说第 1 行: 'a' 附近有语法错误。
    好想它并没有把两个单引号当作一个啊!!!
    救命~~
      

  4.   

    string str=text.text.tostring();
    strsql="select * from table where id='" +str+ "'";
    SqlDataAdapter da=new SqlDataAdapter(strsql,dbcon);
    DataSet ds=new DataSet();
    da.Fill(ds,dstable);
    这样就可以了啊,不晓得你说的是不是这个意思!
      

  5.   

    TO chenyun_424:
    不是你那个意思~是Eddie005(♂) 暴赱 『零零伍』 (︶︵︶) 说的那个意思~但是问题还是没有解决
      

  6.   

    select * from table where id='"+a+"';
    如果a是一个字符,可以这样写:
    select * from table where id='a';
    如果a是一个数字,可以这样写:
    select * from table where id=a;
      

  7.   

    SqlDataAdapter da = new SqlDataAdapter("",conn);
    da.SelectCommand = new SqlCommand("select * from table where Id=@Id", conn);
    da.SelectCommand.Parameters.Add("@Id", SqlDbType.Int, 4, "Id").Value = text.text ;
    DataSet ds = new DataSet();
    da.Fill(ds);
      

  8.   

    to chenyun_424:
    晕。你误会我意思了~我第一个贴把问题说的很明白啊!
      

  9.   

    strsql="select * from table where id=@id";
    SqlDataAdapter.AddParamters("@id",String,text.text);第二行可能不对,但思路就是这样。
    通过添加参数来借据,这样就不怕有'了.
      

  10.   

    to9 hxh8201~~
    哈哈~虽然我没有测试你说的方法~但是我觉得应该是解决问题的办法~谢谢你啊~
    30分是属于你的。。嘻嘻
      

  11.   

    高手我试试了用查询分析器输入select * from table where id =''a''
    它会说第 1 行: 'a' 附近有语法错误。
    好想它并没有把两个单引号当作一个啊!!!
    救命~~
    -----------------------------
    不是这样用的,你要把取到的值里面的单引号替换为两个单引号
    string tmp = ....
    string sql = "select * from table where id = '" + tmp.replace("'","''") + "'"
      

  12.   

    你是不是想查id='a';对字符可以用like:
    select * from table where to_cha(id) like 'a';
      

  13.   

    to hchxxzx:我明白了Eddie005(♂) 暴赱 『零零伍』 (︶︵︶)的意思了~
    由于30少了~每人分点。不好意思!
      

  14.   

    to hchxxzx:我明白了Eddie005(♂) 暴赱 『零零伍』 (︶︵︶)的意思了~
    由于30少了~每人分点。不好意思!
      

  15.   

    别忘记给分啊我在VC里是这么处理的CString query;
    query.Format("Select * from Table where ID='%s'","a");