请问下这段代码因该怎么写啊,比如数据源为dbase格式的?

解决方案 »

  1.   


    CListBox *listbox=(CListBox*)GetDlgItem(IDLST_CHARTS);
    listbox->ResetContent();
    CString strTemp="SELECT * FROM sysobjects WHERE SysObjects.name like 'ly%' "; CoInitialize(NULL);
    _ConnectionPtr pConnection(__uuidof(Connection));
    _RecordsetPtr pRecordset(__uuidof(Recordset)); pConnection->ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;Data Source=SQL_default;Initial Catalog=lydb1";
    pConnection->Open("","","",adConnectUnspecified);
    pRecordset=pConnection->Execute((_bstr_t)strTemp,NULL,adCmdText);

    _bstr_t varOutput;
    _bstr_t varTab("\t");
        _bstr_t varRet("\n");
        _bstr_t varNull("");
    long lRecCount = pRecordset->RecordCount;

    while(!pRecordset->adoEOF)
    {
    listbox->AddString((_bstr_t)pRecordset->GetCollect("name"));
    pRecordset->MoveNext();
    } pRecordset->Close();
    pConnection->Close();
    pConnection.Release();
    pRecordset.Release(); CoUninitialize();----------------------------------------
    如果是查找所有表名,查询语句是strTemp="SELECT * FROM sysobjects";
    另外你数据源是dbase
    那你的ConnectionString和我不一样了,你可以这么获取
    建立一个文本文档,改名为 *.udl
    然后双击打开,后面自己配置要连接的东西
    然后关闭,再用记事本打开,那个字符串就是你编程用到的ConnectionString
      

  2.   

    不需要管dbase还是什么其他格式,只要是用ODBC来调用,那么调用方法就是一样的
    bool   CDBInfoDlg::ListTables(bool   bViews,   bool   bSystemTables) 

    CDatabase   database; 
    CString    type   =   " 'TABLE ' "; 
    int     ret=   -1; 
    HSTMT    hStmt; 
    UCHAR       szName[256]; 
    SDWORD     cbName; 
    CString   names; if(   bViews   ) 
    type   +=   ",   'VIEW ' ";//视图 if(   bSystemTables   ) 
    type   +=   ",   'SYSTEM   TABLE ' ";//系统表 try{ 
    if(   !database.Open(m_strDSN)   ) 
    return   false; 

    catch(...){ 
    AfxMessageBox( "Unable   To   Obtain   Table   Information "); 
    return   false; 
    } SQLAllocStmt(database.m_hdbc,&hStmt); 
    ret   =   SQLTables(hStmt, 
    NULL,SQL_NTS, 
    NULL,SQL_NTS, 
    NULL,SQL_NTS, 
    (unsigned   char   *)type.GetBuffer(0),SQL_NTS); if(ret   ==   SQL_ERROR){ 
    SQLFreeStmt(hStmt,SQL_CLOSE); 
    database.Close(); 
    if(ret   ==     SQL_INVALID_HANDLE   ){ 
    AfxMessageBox( "Invalid   handle "); 
    return   false; 

    AfxMessageBox( "Database   Could   Not   be   Open "); 
    return   false; 

    m_cmbTables.ResetContent(); while(1){ ret   =   SQLFetch(hStmt); 
    if(ret   ==   SQL_NO_DATA_FOUND) 
    break; 
    ret   =   SQLGetData(hStmt,   3,   SQL_C_CHAR,   szName,   TABLE_NAME_LENGTH,   &cbName); names.Format( "%s ",szName); 
    m_cmbTables.AddString(names); 
    } m_cmbTables.SetCurSel(0); SQLFreeStmt(hStmt,SQL_CLOSE); 
    database.Close(); ShowTable(); return   true; 
    }