我现在急着需要登陆的代码(vc++),在本地对远程的SQL Server登陆到其中的一个数据库,并进行数据操作,拜托了!我用在本地建立数据源虽然能连接到SQL Server上,但跳出来来一个SQL Server的小窗口说要输入密码及用户名,怎么办?需要API函数登陆的,不要ADO及OLEDB 的,我是用database连接的

解决方案 »

  1.   

    在本机ODBC中做一个系统DSN如以下程序中为“CSCL90”,连接数据库时将用户名和密码做为参数写到连接函数中,请看以下代码:(以下函数具体定义请见MSDN) m_henv=NULL;//为环境句柄
    m_hdbc=NULL;//为数据库连接句柄
    m_hstmt=NULL;//语句误解柄
    SDWORD cb;
        bIsConnected=FALSE;
        //获得环境句柄
        if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,&m_henv)))
    {/////
       //设置ODBC版本号
           if (SQL_SUCCESS == (rc = ::SQLSetEnvAttr(m_henv,SQL_ATTR_ODBC_VERSION, 
          (SQLPOINTER) SQL_OV_ODBC3,SQL_IS_INTEGER)))
       {////
       //得到数据库连接句柄
       if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &m_hdbc)))
       {///
       //连接数据库DSN=cscl90;UID=cscl90;PWD=cscl90//UID为用户名,PWD为口令
       rc = ::SQLConnect(m_hdbc,(SQLCHAR*)"cscl90",SQL_NTS,
                        (SQLCHAR*)"cscl90", SQL_NTS, (SQLCHAR*)"cscl90", SQL_NTS);
                   if ((SQL_SUCCESS == rc) || (SQL_SUCCESS_WITH_INFO == rc))//连接成功
       {//
       bIsConnected = TRUE;
       }//
       else
       {
       AfxMessageBox("连接数据库服务器失败,无法读取初始化数据!");
       }
       }///
       }////
    }/////
       
      

  2.   

    CDatabase::noOdbcDialog   Do not display the ODBC connection dialog box, regardless of whether enough connection information is supplied.
      

  3.   

    可用DBLIB,要求本机装SQL SERVER客户端
    代码
    //////////////////////////////////////////////////////////////////
    //打开数据库
    // char temp[500];
    LOGINREC      *login;        /* Our login information. */ /* These are the variables used to store the returning data. */// RETCODE        result_code;// long float e_course;
    // int voyage_no;
    // do{ fflush(stdout); /* Initialize DB-Library. */
    if (dbinit() == FAIL)
    exit(ERREXIT);
    if(dbsetversion(DBVERSION_100) == FAIL)
    exit(ERREXIT); /* Install the user-supplied error-handling and message-handling
     * routines. They are defined at the bottom of this source file.
     */
    // dberrhandle((EHANDLEFUNC)err_handler);
    // dbmsghandle((MHANDLEFUNC)msg_handler); /*
    ** Get a LOGINREC structure and fill it with the necessary
    ** login information.
    */ login = dblogin();
    //DBSETLCHARSET(login, "iso_1");
    DBSETLUSER(login, "ship");
    DBSETLPWD(login, "ship123");
    //DBSETLUSER(login, "sa");
    //DBSETLPWD(login, "");

    DBSETLAPP(login, "example1"); /*
    ** Get a DBPROCESS structure for communicating with SQL Server.
    ** A NULL servername defaulyts to the server specified by DSQUERY.
    */ dbproc = dbopen(login, "ShipDbSvr");
    dbproc1 = dbopen(login, "ShipDbSvr");
    //dbproc = dbopen(login, "ydcw_db");
    dbuse(dbproc,"CHISREP");
    dbuse(dbproc1,"CHISREPHIS");

    //dbproc = dbopen(login, "DMU_DATA");
    //dbproc1 = dbopen(login, "DMU_DATA");
    //dbproc = dbopen(login, "ydcw_db");
    //dbuse(dbproc,"chisrep");
    //dbuse(dbproc1,"chisrep");
    //}while(dbproc==NULL || dbproc1 == NULL); /*
    ** We are going to retrieve some information, from a table
    ** named "sysobjects", regarding names of system tables and
    ** stored procedures.
    ** We will submit two queries.  The first finds all the rows 
    ** that describe system tables.  The second finds all the rows
    ** that describe stored procedures.  The program will only look
    ** at the first 10 rows that describe stored procedures.
    */ /* First, put the commands into the command buffer. */// dbcmd(dbproc, "select e_course,  voyage_no from plan_route");
    // dbcmd(dbproc, " where type = 'S' ");
    // dbcmd(dbproc, "select e_course, e_speed, e_time, l_latitude, l_longitude, n_latitude, nav_method, plan_route, r_latitude , r_longitude, rpt_no , voyage_no from plan_route");
    // dbcmd(dbproc, " where type = 'P' "); /*
    ** Sql Server processes the command batch in the following
    ** order:
    **
    ** 1) It will check for syntax errors (i.e., "use database pubs" 
    **    is syntactically incorrect; it should be "use pubs").
    ** 2) The second check is a semantic check (i.e., "select * from 
    **    titels" will be incorrect because the spelling should be 
    **    "titles".)
    ** 3) The third check occurs in the actual execution phase. This 
    **    check involves issues like permissions or memory problems.
    ** 
    ** In the execution phase, dbsqlexec() and dbresults() can return 
    ** the value "SUCCEED", which means there are more commands in the 
    ** batch to process and that that command was successful. A value 
    ** of "FAIL" means that the query failed but there may be more 
    ** commands in the batch to process. A value of "NO_MORE_RESULTS"
    ** means that there are no more commands in the batch to process.
    ** Therefore, the programmer must check the return values after
    ** dbsqlexec() and dbresults(), as illustrated below.
    **
    */ /* Send the commands to SQL Server and start execution. */
    // dbsqlexec(dbproc); /* Process each command until there are no more. */// while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
    // {
    // if (result_code == SUCCEED)
    // {
    /* Bind program variables. */

    /*dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)name);
    dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)type);
    dbbind(dbproc, 3, INTBIND, (DBINT)0, (BYTE *)&id);
    dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)crdate);*/
    // dbbind(dbproc,1,INTBIND,4,(BYTE *)&voyage_no);
    // dbbind(dbproc,1,REALBIND,8,(BYTE *)&e_course);
    /* Print appropriate header for the type of
     * data coming back.
     */ /* Now print the rows. */// }
    // }
    /* Close our connection and exit the program. */// dbexit();
    // exit(STDEXIT);
      

  4.   

    代码示例:(用DBLIB)
    //////////////////////////////////////////////////////////////////
    //打开数据库
    // char temp[500];
    LOGINREC      *login;        /* Our login information. */ /* These are the variables used to store the returning data. */// RETCODE        result_code;// long float e_course;
    // int voyage_no;
    // do{ fflush(stdout); /* Initialize DB-Library. */
    if (dbinit() == FAIL)
    exit(ERREXIT);
    if(dbsetversion(DBVERSION_100) == FAIL)
    exit(ERREXIT); /* Install the user-supplied error-handling and message-handling
     * routines. They are defined at the bottom of this source file.
     */
    // dberrhandle((EHANDLEFUNC)err_handler);
    // dbmsghandle((MHANDLEFUNC)msg_handler); /*
    ** Get a LOGINREC structure and fill it with the necessary
    ** login information.
    */ login = dblogin();
    //DBSETLCHARSET(login, "iso_1");
    DBSETLUSER(login, "ship");
    DBSETLPWD(login, "ship123");
    //DBSETLUSER(login, "sa");
    //DBSETLPWD(login, "");

    DBSETLAPP(login, "example1"); /*
    ** Get a DBPROCESS structure for communicating with SQL Server.
    ** A NULL servername defaulyts to the server specified by DSQUERY.
    */ dbproc = dbopen(login, "ShipDbSvr");
    dbproc1 = dbopen(login, "ShipDbSvr");
    //dbproc = dbopen(login, "ydcw_db");
    dbuse(dbproc,"CHISREP");
    dbuse(dbproc1,"CHISREPHIS");

    //dbproc = dbopen(login, "DMU_DATA");
    //dbproc1 = dbopen(login, "DMU_DATA");
    //dbproc = dbopen(login, "ydcw_db");
    //dbuse(dbproc,"chisrep");
    //dbuse(dbproc1,"chisrep");
    //}while(dbproc==NULL || dbproc1 == NULL); /*
    ** We are going to retrieve some information, from a table
    ** named "sysobjects", regarding names of system tables and
    ** stored procedures.
    ** We will submit two queries.  The first finds all the rows 
    ** that describe system tables.  The second finds all the rows
    ** that describe stored procedures.  The program will only look
    ** at the first 10 rows that describe stored procedures.
    */ /* First, put the commands into the command buffer. */// dbcmd(dbproc, "select e_course,  voyage_no from plan_route");
    // dbcmd(dbproc, " where type = 'S' ");
    // dbcmd(dbproc, "select e_course, e_speed, e_time, l_latitude, l_longitude, n_latitude, nav_method, plan_route, r_latitude , r_longitude, rpt_no , voyage_no from plan_route");
    // dbcmd(dbproc, " where type = 'P' "); /*
    ** Sql Server processes the command batch in the following
    ** order:
    **
    ** 1) It will check for syntax errors (i.e., "use database pubs" 
    **    is syntactically incorrect; it should be "use pubs").
    ** 2) The second check is a semantic check (i.e., "select * from 
    **    titels" will be incorrect because the spelling should be 
    **    "titles".)
    ** 3) The third check occurs in the actual execution phase. This 
    **    check involves issues like permissions or memory problems.
    ** 
    ** In the execution phase, dbsqlexec() and dbresults() can return 
    ** the value "SUCCEED", which means there are more commands in the 
    ** batch to process and that that command was successful. A value 
    ** of "FAIL" means that the query failed but there may be more 
    ** commands in the batch to process. A value of "NO_MORE_RESULTS"
    ** means that there are no more commands in the batch to process.
    ** Therefore, the programmer must check the return values after
    ** dbsqlexec() and dbresults(), as illustrated below.
    **
    */ /* Send the commands to SQL Server and start execution. */
    // dbsqlexec(dbproc); /* Process each command until there are no more. */// while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
    // {
    // if (result_code == SUCCEED)
    // {
    /* Bind program variables. */

    /*dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)name);
    dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)type);
    dbbind(dbproc, 3, INTBIND, (DBINT)0, (BYTE *)&id);
    dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT)0, 
    (BYTE DBFAR *)crdate);*/
    // dbbind(dbproc,1,INTBIND,4,(BYTE *)&voyage_no);
    // dbbind(dbproc,1,REALBIND,8,(BYTE *)&e_course);
    /* Print appropriate header for the type of
     * data coming back.
     */ /* Now print the rows. */// }
    // }
    /* Close our connection and exit the program. */// dbexit();
    // exit(STDEXIT);