大家好 我想问个问题,根据编辑框输入的条件 把输入值赋给一个字符串变量str  在SQL语句进行查询的时候 查询的条件该怎么写呢

解决方案 »

  1.   

    CString sql;
    sql.Format("SELECT * FROM [table_name]
    WHERE 字段名1= '%s' AND 字段名2 = %d",str1,str2);
      

  2.   

    楼上正解,你的str就是他的str1或str2
      

  3.   

    我还是不知道怎么弄呢
    我的语句是这样的
    try
    {
      m_pRecordset.CreateInstance("ADODB.Recordset");
      m_pRecordset->Open("SELECT * FROM DClass,DInformation WHERE DClass.D_Class = DInformation.D_Class AND  DClass.D_type=",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);m_bSuccess = TRUE;
    m_pRecordset->MoveFirst();
    在这里的话 要怎么修改呢 
    DClass.D_type=  我要把str 赋值他
      

  4.   

    cstring strsql;
    strsql.format(“SELECT * FROM DClass,DInformation WHERE DClass.D_Class = DInformation.D_Class AND DClass.D_type= %s”,str);m_pRecordset->Open(strsql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
      

  5.   

    你要看你那个值是什么类型,如果是字符形要加上‘’号,如果是数值形就不加,你可以将它的SQL语句复制到你的SQL中执行下就知道了,你说的情况我估计是字符形没加''号
      

  6.   

    先去数据库中尝试你的SQL语句能否正常运行。先去检查数据库是否正常连接。
      

  7.   

    还有你的数据库连接是否正常,自己加上这段HRESULT hr; 
    try 

    hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 
    if(SUCCEEDED(hr)) 

    hr = m_pConnection->Open(_T("Provider=SQLOLEDB;Server=XP-201103191332;Database=TEXT;User ID=sa;pwd=123456;Integrated Security=SSPI "),_T(""),_T(""),adModeUnknown);///连接数据库



    catch( _com_error e)///捕捉异常 

    CString errormessage; 
    errormessage.Format(_T("连接数据库失败!\r\n错误信息:%s"),e.ErrorMessage()); 
    AfxMessageBox(errormessage);///显示错误信息 
    } 你根据自己的改,别直接复制,我给的是例子。
      

  8.   

    谢谢啊 我数据库连接正常 语句也正确 就是m_pRecordset->Open里面的 __vfptr CXX0030: 错误: 无法计算表达式的值 这个错误,有些不知所措啊
      

  9.   

    strsql.Format(_T("SELECT * FROM DClass,DInformation WHERE DClass.D_Class = DInformation.D_Class AND  DClass.D_UsingType = '%s'"),str);
    m_pRecordset->Open((_variant_t &)strsql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);这个是我按着 5楼的那个 改的 就是会中断
      

  10.   

    CString strsql="SELECT * FROM DClass,DInformation WHERE DClass.D_Class = DInformation.D_Class AND DClass.D_type='"+str+"';";
    try
    {
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pRecordset->Open(_bstr_t(strsql),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
    }
    catch(_com_error e)
    {
    AfxMessageBox(e.Description());
    }
    return m_pRecordset;
      

  11.   

    m_pRecordset->Open((_bstr_t)strsql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);这样看下?
      

  12.   

    m_pRecordset->Open((_bstr_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);