小弟遇到一个问题,vc++6.0连接access数据库的,这个数据库由于设制了工作组(*.mdw)且设定了用户名和密码,数据库(*.mdb)本身也有一个密码,请各位高手指点一下如何连接此类数据库,举个例子什么的。有demo更完美(可发到[email protected])。本人实在愚钝,在此先谢谢各位了。

解决方案 »

  1.   

    HRESULT ExecuteSqlCommand( LPCTSTR lpTable, LPCTSTR lpSqlExecute )
    {
    BOOL bSuccess = FALSE; if( lpTable == NULL || lpSqlExecute == NULL )
    return E_FAIL; IADORecordBinding   *picRs = NULL;
    _RecordsetPtr pRs("ADODB.Recordset");//初始化记录查询
    _ConnectionPtr pConn("ADODB.Connection" );//初始化记录连接
    _CommandPtr     pCmdChange  = NULL; CString  strSql,chSqlSentence; strSql = strSql +
    _T("Provider=Microsoft.Jet.OLEDB.4.0;") +
    _T("Data Source = ") + 
    m_szMdbPath +
    _T(";Persist Security Info=False;Jet OLEDB:Database Password=") +
    m_lpPass; try
    {
    pConn->Open((_bstr_t)strSql, "", "", adModeUnknown);//打开数据库
    pRs->QueryInterface(
    __uuidof(IADORecordBinding), (LPVOID*)&picRs); TESTHR(pCmdChange.CreateInstance(__uuidof(Command)));
    pCmdChange->ActiveConnection = pConn;
    pCmdChange->CommandText = lpSqlExecute; // Open titles table, casting Connection pointer to an 
    // IDispatch type so converted to correct type of variant.
    TESTHR(pRs.CreateInstance(__uuidof(Recordset)));
    pRs->Open ((_variant_t)lpTable, _variant_t((IDispatch *) pConn, true), adOpenStatic, adLockOptimistic, adCmdTable);
    // Clear extraneous errors from the Errors collection.
    pConn->Errors->Clear(); // Use the Connection object's execute method to
    // execute SQL statement to restore data.
    pConn->Execute(lpSqlExecute, NULL, adExecuteNoRecords); // Retrieve the current data by requerying the recordset.
    pRs->Requery(adCmdUnknown); //pRs->Update();
    pRs->Close(); bSuccess = TRUE;
    }
    catch (...)
    {
    if ( pRs != NULL )
    pRs->Close(); if ( pConn != NULL )
    pConn->Close(); pRs = NULL;
    pConn = NULL;
    return E_FAIL;
    } pConn->Close();
    return S_OK;
    }
      

  2.   

    m_database->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Administrator\\Project\\shujuku.mdb;Persist Security Info=False;Jet OLEDB:Database Password=168168","","",adModeUnknown);