细节:
1、访问本机与访问其它机器的区别;
2、机器名:dbserver,(ip:192.168.0.1),数据库名:mydb,登录名:user,口令:pwd。连接后需要对其进行读写。
3、分数不够,我再加!

解决方案 »

  1.   


    AfxOleInit();
    Readini(); CString strDsn;
    strDsn="Provider=SQLOLEDB;Server=" + m_ipaddress + ";Database= Gps"; m_pConnection.CreateInstance("ADODB.Connection");
    try
    {
        m_pConnection->Open(_bstr_t(strDsn), "user", "pwd", adModeUnknown);//adConnectUnspecified
    }
    catch(_com_error e){//若出现错误则处理后返回
       CString errormessage;
       errormessage.Format("登录失败!\r\n错误信息:%s\r\n%s %s",e.ErrorMessage(),strDsn, (LPCTSTR)e.Description());
       AfxMessageBox(errormessage);
    //    if(pConnection!=NULL && pConnection->State){
    //   pConnection->Close();
    //    }
    //    m_pConnection=NULL;
       return FALSE;
    }
    以上是连接代码
    错误信息如下:
    登录失败!
    错误信息:未指定的错误
    [DBNMPNTW]ConnectionOpen(CreateFile()).
      

  2.   

    CString strSQL;
    CString strSQLServerName = "dbserver";
    CString strDatabaseName = "mydb";
    CString strLoginUsrName = "user";
    CString strLoginPWD = "pwd";
    HRESULT m_hr;
    _RecordsetPtr m_pRs;
    _ConnectionPtr m_pCn;
    try
    {
    m_hr=m_pCn.CreateInstance("ADODB.Connection");
    if(SUCCEEDED(m_hr)) 
    {
    m_pCn->ConnectionTimeout=30;
    m_pCn->Mode=adModeReadWrite;
    strSQL.Format("Provider=SQLOLEDB.1;Data Source=%s;Initial Catalog=%s;User ID=%s;PWD=%s",
    strSQLServerName,strDatabaseName,strLoginUsrName,strLoginPWD);
    m_hr = m_pCn->Open(_bstr_t(strSQL),"","",adModeUnknown);
    m_hr = m_pRs.CreateInstance("ADODB.Recordset");
    }
    } catch(_com_error e)///捕捉异常 
    {
    return false;
    } //...............操作数据库.....................
      

  3.   


    HRESULT m_hr;
    m_hr = g_pRecordset.CreateInstance("ADODB.Recordset");
    if(SUCCEEDED(m_hr)) 
    {
    try
    {
    m_hr = g_pRecordset->Open("SELECT * FROM Trail", // 查询Trail表中所有字段
    theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    }
    catch(_com_error e)
    {
       CString errormessage;
       errormessage.Format("登录失败!\r\n错误信息:%s\r\n %s",e.ErrorMessage(), (LPCTSTR)e.Description());
       AfxMessageBox(errormessage);
    }
    }
    else
    {
    AfxMessageBox("Error");
    return FALSE;
    }
    程序运行到m_pRecordset->open时出内存访问错误
      

  4.   

    //连接数据库
    HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
    if(FAILED(hr))return FALSE;_bstr_t strConnect="Provider=SQLOLEDB; Server=dbserver;"; strConnect+="Database=mydb; uid=user; pwd=pwd;";      //connecting to the database server now:
       try{pMyConnect->Open(strConnect,"","",NULL);}
       catch (_com_error &e)
       {
       ::MessageBox(NULL,e.Description(),"警告",MB_OK | MB_ICONWARNING);
       }
       BeginWaitCursor();  
       _RecordsetPtr pList;
       hr=pList.CreateInstance(__uuidof(Recordset));
       if(FAILED(hr))return FALSE; 
       //取DSSW_INPUTFORM中记录集,包含了界面名称,主表,过滤条件,排序字段,流程标志字段
       _bstr_t strQuery="select formname from DSSW_INPUTFORM order by formname";
       hr=pList->Open(strQuery,_variant_t((IDispatch *) pMyConnect, true),
                adOpenDynamic, adLockOptimistic, adCmdText);
       if(FAILED(hr))return FALSE;
       // 初始化列表框
       VARIANT_BOOL ValEof;
       ValEof=pList->adoEOF;
       while(!ValEof)
       {  
       _bstr_t value=pList->Fields->GetItem((_variant_t)short(0))->Value;
       m_listFormname.InsertString(-1,(LPCTSTR)value);
       pList->MoveNext();
       ValEof=pList->adoEOF;
       }
       EndWaitCursor();
      

  5.   

    HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
    if(FAILED(hr))return FALSE;_bstr_t strConnect="Provider=SQLOLEDB; Server=dbserver;"; strConnect+="Database=mydb; uid=user; pwd=pwd;";      //connecting to the database server now:
       try{pMyConnect->Open(strConnect,"","",NULL);}
       catch (_com_error &e)
       {
       ::MessageBox(NULL,e.Description(),"警告",MB_OK | MB_ICONWARNING);
       }
       BeginWaitCursor();  
       _RecordsetPtr pList;
       hr=pList.CreateInstance(__uuidof(Recordset));
       if(FAILED(hr))return FALSE; 
       //取DSSW_INPUTFORM中记录集,包含了界面名称,主表,过滤条件,排序字段,流程标志字段
       _bstr_t strQuery="select formname from DSSW_INPUTFORM order by formname";
       hr=pList->Open(strQuery,_variant_t((IDispatch *) pMyConnect, true),
                adOpenDynamic, adLockOptimistic, adCmdText);
       if(FAILED(hr))return FALSE;
       // 初始化列表框
       VARIANT_BOOL ValEof;
       ValEof=pList->adoEOF;
       while(!ValEof)
       {  
       _bstr_t value=pList->Fields->GetItem((_variant_t)short(0))->Value;
       m_listFormname.InsertString(-1,(LPCTSTR)value);
       pList->MoveNext();
       ValEof=pList->adoEOF;
       }
       EndWaitCursor();
      

  6.   

    还有把:
    if(!AfxOleInit())  // Your addition starts here.
    {
    AfxMessageBox("Could not initialize COM dll");
    return FALSE;
    // End of your addition.
    }
    加到阿CXApp::InitInstance()
    的开头