谢谢

解决方案 »

  1.   

    用连接字符串,顺便问一下,为什么还用Cdatabase类连接数据库而不用ADO,连接?下面是ADO的连接:
    CString         strTmp;
    //    CString         SQLServerName;
        strTmp="driver={sql server};server=199.8.8.8;Database=db;UID=sa;PWD=;");
        // Create Connection Object (1.5 Version)
    _variant_t  vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
    _variant_t  vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
    _bstr_t     bstrEmpty(L""); try
    {
              Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
              Conn1->ConnectionString =(LPCTSTR) strTmp;// bstrSQLServerConnect;
              Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
      return 0;
    }
    catch(_com_error & ce)
    {
    //使用另一种方式登陆
    strTmp.Format("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=%s",
    DBName);
    try
    {
      Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
      Conn1->ConnectionString =(LPCTSTR) strTmp;// bstrSQLServerConnect;
      Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
      return 0;
    }
    catch(_com_error & ce)
    {
    #ifdef _DEBUG
    _bstr_t bstrDescription(ce.Description ());
    CString strError;
    strError=bstrDescription.copy();
    g_pMgr->ShowMsg(99,"Error Code=%s At Line=%4d File=%s",
    strError,
    __LINE__,
    __FILE__ );
    #endif }
    #ifdef _DEBUG
    _bstr_t bstrDescription(ce.Description ());
    CString strError;
    strError=bstrDescription.copy();
    g_pMgr->ShowMsg(99,"Error Code=%s At Line=%4d File=%s",
    strError,
    __LINE__,
    __FILE__ );
    #endif
    return 1;
    }
      

  2.   

    #include "sql.h"
    #include <odbcinst.h>CDatabase m_gDatabase;//the global variable used to link remote database!
    void CMainFrame::OnLinkLocalhost() 
    {
    // TODO: Add your command handler code here  
      RETCODE retcode; 
      char    *szDriver = "SQL Server";
      char    *szAttributes =
      "DSN=DB_DSN\0DESCRIPTION=DSN used for the Visual  C++ database test!\0"
      "SERVER=199.8.8.8\\your_sqlserver_name\0"//if you have only on sqlserver you can just use the ip
      "DATABASE=Db\0";
      //first create the data source in localhost://data source is:DB_DSN
      retcode = SQLConfigDataSource(NULL,
                                 ODBC_ADD_DSN,
                                 szDriver,
                                 szAttributes);
      if(retcode)
    MessageBox("Success!");
      else
    {
    MessageBox("Failed");
    return;
    }
      
      //use the data source :DB_DSN,change the PWD=password to your own passward
      if(m_gDatabase.OpenEx( _T( "DSN=DB_DSN;UID=sa;PWD=password" ),
                     CDatabase::openReadOnly |
                     CDatabase::noOdbcDialog ))
      {
      
      try
      {
      m_gDatabase.ExecuteSQL(_T(" select * from your_table"));
      }  catch(CDBException e)
      {
       MessageBox("Failed to execute the sql commands");
       return;
      }
      }
      else
       {
       MessageBox("Failed to connect the remote database!") ;
       return ;
       }
    }
    -----------------------
    [email protected]