SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0","DSN=SunwaveRepeaterOMC DBQ=pub.mdb");

解决方案 »

  1.   

    A. 使用 SQLConfigDataSource 创建数据源
    #include <stdio.h>
    #include <windows.h>
    #include "sql.h"
    #include <odbcinst.h>int main()
    {
    RETCODE retcode;UCHAR   *szDriver = "SQL Server";
    UCHAR   *szAttributes =
    "DSN=MyDSN\0DESCRIPTION=SQLConfigDSN Sample\0"
    "SERVER=MySQL\0ADDRESS=MyServer\0NETWORK=dbmssocn\0"
    "DATABASE=pubs\0";retcode = SQLConfigDataSource(NULL,
                           ODBC_ADD_DSN,
                           szDriver,
                           szAttributes);B. 创建文件数据源
    在 SQLDriverConnect 中使用 SAVEFILE 关键字创建文件数据源,然后使用 SQLDriverConnect 与该文件数据源进行连接。这是删除错误处理后的简化示例。#include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <sql.h>
    #include <sqlext.h>
    #include <odbcss.h>#define MAXBUFLEN   255SQLHENV      henv = SQL_NULL_HENV;
    SQLHDBC      hdbc1 = SQL_NULL_HDBC;int main() {   RETCODE      retcode;   // This format of the SAVEFILE keyword saves a successful
       // connection as the file Myfiledsn.dsn in the ODBC default
       // directory for file DSNs.
       SQLCHAR      szConnStrIn[MAXBUFLEN] =
                "SAVEFILE=MyFileDSN;DRIVER={SQL Server};SERVER=MySQL;"
                "NETWORK=dbmssocn;UID=sa;PWD=MyPassWord;";   SQLCHAR      szConnStrOut[MAXBUFLEN];
       SQLSMALLINT   cbConnStrOut = 0;    // Allocate the ODBC Environment and save handle.
       retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);   //Notify ODBC that this is an ODBC 3.0 application.
       retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
                         (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);   // Allocate an ODBC connection handle and connect.
       retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
       retcode = SQLDriverConnect(hdbc1,      // Connection handle
                         NULL,         // Window handle
                         szConnStrIn,      // Input connect string
                         SQL_NTS,         // Null-terminated string
                         szConnStrOut,   // Addr of output buffer
                         MAXBUFLEN,      // Size of output buffer
                         &cbConnStrOut,   // Address of output length
                         SQL_DRIVER_NOPROMPT);   // Disconnect, set up a new connect string, and then test file DSN.
       SQLDisconnect(hdbc1);
       strcpy(szConnStrIn, "FILEDSN=MyFileDSN;UID=sa;PWD=MyPassWord;");
       retcode = SQLDriverConnect(hdbc1,      // Connection handle
                         NULL,         // Window handle
                         szConnStrIn,      // Input connect string
                         SQL_NTS,         // Null-terminated string
                         szConnStrOut,   // Addr of output buffer
                         MAXBUFLEN,      // Size of output buffer
                         &cbConnStrOut,   // Address of output length
                         SQL_DRIVER_NOPROMPT);   /* Clean up. */
       SQLDisconnect(hdbc1);
       SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
       SQLFreeHandle(SQL_HANDLE_ENV, henv);
       return(0);
    }