怎样在ADO打开数据库后进行查询??
我在一个窗体中需要打开几个有关联的表,具体就是窗体显示一个表的 内容后,需要由这个表中的某项属性来决定打开另外的表,显示另外表的相关内容。我不知道查询怎么实现

解决方案 »

  1.   

    为什么不将几个表连接成视图?
    至于打开数据库后如何查询,下面是我的例子:
    strQuery.Format("select * from %s",strTableName);
    _bstr_t query=strQuery;
    _bstr_t source=m_strSource;
    try
    {
    hr=m_recordset->Open(query,_variant_t((IDispatch *)m_connection,true),adOpenDynamic,adLockOptimistic,adCmdText);
    }
    catch(_com_error &e)
    {
    AfxMessageBox(e.Description());
    return FALSE;
    }
    return (SUCCEEDED(hr));
    显示:
    CListCtrl &listCtrl=GetListCtrl();
    listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
    CString strColName;
    Fields *fields=NULL;
    try
    {
    hr=m_recordset->get_Fields(&fields);
    if(SUCCEEDED(hr))
    hr=fields->get_Count((long *)pulColCount);
    for(long i=0;i<(*pulColCount);i++)
    {
    BSTR bstrColName;
    hr=fields->Item[i]->get_Name(&bstrColName);
    strColName=bstrColName;
    int nWidth=listCtrl.GetStringWidth(strColName)+20;
    listCtrl.InsertColumn(i,strColName,LVCFMT_LEFT,nWidth);
    }
    if(SUCCEEDED(hr))
    fields->Release();
    }
    catch(_com_error &e)
    {
    AfxMessageBox(e.Description());
    return FALSE;
    }
      

  2.   

    在上面的strQuery.Format("select * from %s",strTableName);我想要是类似以下样子,有条件的查询,好像就不行
    strQuery.Format("select * from %s where 属性=%s",strTableName,Name);
      

  3.   

    strQuery.Format("select * from %s  where strTableNames .属性=%s",strTableNames ,属性Name);然后执行
    m_recordset->Open(query,_variant_t((IDispatch *)m_connection,true),adOpenDynamic,adLockOptimistic,adCmdText);
    就会出现 Runtine ERROR!
    但是如果是
    strQuery.Format("select * from %s",strTableName);就是没有后面的where 条件就可以
    这到底是什么原因?
      

  4.   

    strQuery.Format("select * from %s where 属性= '%s'",strTableName,Name);