游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2444|回复: 1

无题

[复制链接]

17

主题

1629

帖子

5982

积分

论坛元老

Rank: 8Rank: 8

积分
5982
QQ
发表于 2016-1-9 12:02:16 | 显示全部楼层 |阅读模式
  1. // Connecting_with_SQLConnect.cpp
  2. // compile with: user32.lib odbc32.lib
  3. #include <windows.h>
  4. #include <sqlext.h>
  5. #include <mbstring.h>
  6. #include <stdio.h>
  7. #include <sqltypes.h>

  8. #define MAX_DATA 100
  9. #define MYSQLSUCCESS(rc) ((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO) )

  10. class direxec {
  11.    RETCODE rc; // ODBC return code
  12.    HENV henv; // Environment   
  13.    HDBC hdbc; // Connection handle
  14.    HSTMT hstmt; // Statement handle

  15.    unsigned char szData[MAX_DATA]; // Returned data storage
  16.    SDWORD cbData; // Output length of data
  17.    unsigned char chr_ds_name[SQL_MAX_DSN_LENGTH]; // Data source name

  18. public:
  19.    direxec(); // Constructor
  20.    void sqlconn(); // Allocate env, stat, and conn
  21.    void sqlexec(); // Execute SQL statement
  22.    void sqldisconn(); // Free pointers to env, stat, conn, and disconnect
  23.    void error_out(); // Displays errors
  24. };

  25. // Constructor initializes the string chr_ds_name with the data source name.
  26. // "Northwind" is an ODBC data source (odbcad32.exe) name whose default is the Northwind database
  27. direxec::direxec() {
  28.    _mbscpy_s(chr_ds_name, SQL_MAX_DSN_LENGTH, (const unsigned char *)"helloworld");
  29. }

  30. // Allocate environment handle and connection handle, connect to data source, and allocate statement handle.
  31. void direxec::sqlconn() {
  32.    SQLAllocEnv(&henv);
  33.    SQLAllocConnect(henv, &hdbc);
  34.    rc = SQLConnect(hdbc, chr_ds_name, SQL_NTS, NULL, 0, NULL, 0);

  35.    // Deallocate handles, display error message, and exit.
  36.    if (!MYSQLSUCCESS(rc)) {
  37.       SQLFreeConnect(henv);
  38.       SQLFreeEnv(henv);
  39.       SQLFreeConnect(hdbc);
  40.       if (hstmt)
  41.          error_out();
  42.       exit(-1);
  43.    }

  44.    rc = SQLAllocStmt(hdbc, &hstmt);
  45. }

  46. // Execute SQL command with SQLExecDirect() ODBC API.
  47. void direxec::sqlexec() {

  48.         SQLLEN mAffectCount;
  49.         SQLSMALLINT mColCount;

  50.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (1, 'HeFei', 'IDN', 'AnHui', 10001)", SQL_NTS);
  51.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (2, 'BengBu', 'IDN', 'AnHui', 10002)", SQL_NTS);
  52.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (3, 'SuZhou', 'IDN', 'AnHui', 10003)", SQL_NTS);
  53.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"UPDATE  city SET Name = 'HuaiNan' WHERE Name = 'HeFei'", SQL_NTS);
  54.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"DELETE FROM city WHERE Name = 'HuaiNan'", SQL_NTS);

  55.         SQLRowCount(hstmt,&mAffectCount);
  56.         SQLNumResultCols(hstmt,&mColCount);
  57.         SQLCloseCursor(hstmt);
  58.         SQLFreeStmt(hstmt, SQL_UNBIND);

  59.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"SELECT * FROM city", SQL_NTS);
  60.         SQLRowCount(hstmt,&mAffectCount);
  61.         SQLNumResultCols(hstmt,&mColCount);

  62.         int ColIndex;
  63.         char Col[100][2048] = {0};
  64.         SQLCHAR        ColName[100][2048] = {0};
  65.         SQLLEN mCollocate[100] = {0};

  66.         for (ColIndex=0;ColIndex<mColCount;ColIndex++)
  67.         {
  68.                 SQLBindCol(hstmt,ColIndex+1,SQL_C_CHAR,Col[ColIndex],2048,&mCollocate[ColIndex]);
  69.                 SQLDescribeCol(hstmt,ColIndex+1,ColName[ColIndex],30,NULL,NULL,NULL,NULL,NULL);
  70.         }

  71.         puts("SQLFetch");

  72.         while ((rc = SQLFetch(hstmt)) != SQL_NO_DATA)
  73.         {
  74.                 printf("%s %s %s %s %s\n", Col[0], Col[1], Col[2], Col[3], Col[4]);
  75.         }

  76.         SQLCloseCursor(hstmt);
  77.         SQLFreeStmt(hstmt, SQL_UNBIND);

  78.         rc = SQLExecDirect(hstmt, (SQLCHAR*)"SELECT * FROM city", SQL_NTS);
  79.         SQLRowCount(hstmt,&mAffectCount);
  80.         SQLNumResultCols(hstmt,&mColCount);

  81.         puts("\nSQLGetData");
  82.         memset(Col, 0, 100*2048*sizeof(char));

  83.         for (int RowIndex=0;RowIndex<mAffectCount;RowIndex++)
  84.         {
  85.                 SQLFetch(hstmt);

  86.                 for (int ColIndex=0;ColIndex<mColCount;ColIndex++)
  87.                 {
  88.                         int        TotalGet = 0;
  89.                         SQLLEN LenData = 0;
  90.                         int        Ret        = 0;

  91.                         while(Ret=SQLGetData(hstmt, ColIndex+1, SQL_C_CHAR,Col[ColIndex]+TotalGet,200000,&LenData)!=SQL_NO_DATA)
  92.                         {
  93.                                 break;
  94.                         }
  95.                 }

  96.                 printf("%s %s %s %s %s\n", Col[0], Col[1], Col[2], Col[3], Col[4]);
  97.         }
  98. }

  99. // Free the statement handle, disconnect, free the connection handle, and free the environment handle.
  100. void direxec::sqldisconn() {
  101.    SQLFreeStmt(hstmt,SQL_DROP);
  102.    SQLDisconnect(hdbc);
  103.    SQLFreeConnect(hdbc);
  104.    SQLFreeEnv(henv);
  105. }

  106. // Display error message in a message box that has an OK button.
  107. void direxec::error_out() {
  108.    unsigned char szSQLSTATE[10];
  109.    SDWORD nErr;
  110.    unsigned char msg[SQL_MAX_MESSAGE_LENGTH + 1];
  111.    SWORD cbmsg;

  112.    while (SQLError(0, 0, hstmt, szSQLSTATE, &nErr, msg, sizeof(msg), &cbmsg) == SQL_SUCCESS) {
  113.       sprintf_s((char *)szData, sizeof(szData), "Error:\nSQLSTATE=%s, Native error=%ld, msg='%s'", szSQLSTATE, nErr, msg);
  114.       MessageBox(NULL, (const char *)szData, "ODBC Error", MB_OK);
  115.    }
  116. }

  117. int main () {
  118.    direxec x;   // Declare an instance of the direxec object.
  119.    x.sqlconn();   // Allocate handles, and connect.
  120.    x.sqlexec();   // Execute SQL command
  121.    x.sqldisconn();   // Free handles and disconnect
  122.    getchar();
  123. }
复制代码

17

主题

1629

帖子

5982

积分

论坛元老

Rank: 8Rank: 8

积分
5982
QQ
 楼主| 发表于 2016-1-10 11:39:36 | 显示全部楼层
给个主页推荐一下呗
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 12:16

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表