CDatabase db;
db.OpenEx("ODBC;DSN=testdb;user=sa;pwd=pass");CSockSet set((&db));
set.Open(...);
set.close();db.Close();

解决方案 »

  1.   

    我这里怎么不是这样的
    我一直在用你这样的语句访问sql server.
    怎么不弹出对话框。
    是不是你的用户名称和密码不对,
    我在::GetDefaultConnect()中只有
    "ODBC;DSN=testdb”;
      

  2.   

    饶过odbc口令的方法:
    1.return _T("ODBC")
      系统提是对话框让你选择数据源等信息。
    2。return _T("ODBC;DSN=mydsn")
      需要用户和密码
    3。return _T("ODBC;DSN=mydsn;UID=myid")
      需要密码
    4。return _T("ODBC;DSN=mydsn;UID=myid;PWD=mypwd")
      用户提供了所有的信息。
      

  3.   

    请查CDatabase::OpenEx函数,
    CDatabase::OpenEx
    virtual BOOL OpenEx( LPCTSTR lpszConnectString, DWORD dwOptions = 0 );
    throw( CDBException, CMemoryException );Return ValueNonzero if the connection is successfully made; otherwise 0 if the user chooses Cancel when presented a dialog box asking for more connection information. In all other cases, the framework throws an exception.ParameterslpszConnectStringSpecifies an ODBC connect string. This includes the data source name as well as other optional information, such as a user ID and password. For example, "DSN=SQLServer_Source;UID=SA;PWD=abc123" is a possible connect string. Note that if you pass NULL for lpszConnectString, a Data Source dialog box will prompt the user to select a data source.dwOptionsA bitmask which specifies a combination of the following values. The default value is 0, meaning that the database will be opened as shared with write access, the ODBC Cursor Library DLL will not be loaded, and the ODBC connection dialog box will display only if there is not enough information to make the connection. CDatabase::openExclusive   Not supported in this version of the class library. A data source is always opened as shared (not exclusive). Currently, an assertion fails if you specify this option.
    CDatabase::openReadOnly   Open the data source as read-only.
    CDatabase::useCursorLib   Load the ODBC Cursor Library DLL. The cursor library masks some functionality of the underlying ODBC driver, effectively preventing the use of dynasets (if the driver supports them). The only cursors supported if the cursor library is loaded are static snapshots and forward-only cursors. If you plan to create a recordset object directly from CRecordset without deriving from it, you should not load the cursor library.
    *****请注意这里******
    CDatabase::noOdbcDialog   Do not display the ODBC connection dialog box, regardless of whether enough connection information is supplied.
    CDatabase::forceOdbcDialog   Always display the ODBC connection dialog box.