CString strSql="select * from table1 where='111'";
这样是能执行成功,并返回记录集
CSring a="111";
CString strSql="select * from table1 where=\'%a\'";
这样编译是没出错,但是  好象返回记录集为空的.
这是为什么呢?
求  [where]的具体写法~

解决方案 »

  1.   

    呵呵,你这第2句有问题,你实际上是在执行“select * from table1 where=\'%a\'”, a并没有替换。这样做:
    CString strSql="select * from table1 where='%"+a+"'";
      

  2.   

    或者:strSql.Format("select * from table1 where='%%s'",(LPCTSTR)a);
      

  3.   

    找本SQL的书看看,别这么懒。
      

  4.   

    你的sql语句不对的阿,模糊查询的话要这样的:select * from table1 where col_name like '%a'
      

  5.   

    CSring a="111";
    CString strSql.Format("select * from table1 where='%s'",a);
      

  6.   

    好像有问题,这样:
    1、CString strSql="select * from table1 where 字段名 like '%"+a+"'";
    2、strSql.Format("select * from table1 where 字段名 like '%%s'",(LPCTSTR)a);
      

  7.   

    CString a="111";
    a.Replace("'","''");
    CString strSql="";
    strSql.Format("select * from table1 where='%s'",a);
      

  8.   

    同意 zfive5(醉马不肖) 的作法
      

  9.   

    CSring a="111";
    CString strSql.Format("select * from table1 where='%s'",a);
    copy  zfive5(醉马不肖)
      

  10.   

    m_sShow1="100002"; CString Sql="";
    Sql.Format("SELECT * FROM account where account like '%s'",m_sShow1);
    try{
    m_pRecordset.CreateInstance("ADODB.Recordset");
    ==> m_pRecordset->Open(Sql, _variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    return 1;
    }
    --------------------Configuration: ATMSERVER - Win32 Debug--------------------
    Compiling...
    ATMSERVERDlg.cpp
    G:\SOCK\ATMSERVER\ATMSERVERDlg.cpp(339) : error C2664: 'Open' : cannot convert parameter 1 from 'class CString' to 'const class _variant_t &'
            Reason: cannot convert from 'class CString' to 'const class _variant_t'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.ATMSERVER.exe - 1 error(s), 0 warning(s)
      

  11.   

    m_sShow1="100002"; CString Sql="";
    Sql.Format("SELECT * FROM account where account like '%s'",m_sShow1);
                     BSTR bstrSQL = Sql.AllocSysString();/////////////////添加后试试
    try{
    m_pRecordset.CreateInstance("ADODB.Recordset");
    ==> m_pRecordset->Open(bstrSQL, _variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    return 1;
    }
      

  12.   

    fanlin00428() ( ) 信誉:100 
    调试成功了,
    能告诉我这是为什么呢 ??
    下次可能还会在犯错的(寒~~~~~)