|
- // Connecting_with_SQLConnect.cpp
- // compile with: user32.lib odbc32.lib
- #include <windows.h>
- #include <sqlext.h>
- #include <mbstring.h>
- #include <stdio.h>
- #include <sqltypes.h>
- #define MAX_DATA 100
- #define MYSQLSUCCESS(rc) ((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO) )
- class direxec {
- RETCODE rc; // ODBC return code
- HENV henv; // Environment
- HDBC hdbc; // Connection handle
- HSTMT hstmt; // Statement handle
- unsigned char szData[MAX_DATA]; // Returned data storage
- SDWORD cbData; // Output length of data
- unsigned char chr_ds_name[SQL_MAX_DSN_LENGTH]; // Data source name
- public:
- direxec(); // Constructor
- void sqlconn(); // Allocate env, stat, and conn
- void sqlexec(); // Execute SQL statement
- void sqldisconn(); // Free pointers to env, stat, conn, and disconnect
- void error_out(); // Displays errors
- };
- // Constructor initializes the string chr_ds_name with the data source name.
- // "Northwind" is an ODBC data source (odbcad32.exe) name whose default is the Northwind database
- direxec::direxec() {
- _mbscpy_s(chr_ds_name, SQL_MAX_DSN_LENGTH, (const unsigned char *)"helloworld");
- }
- // Allocate environment handle and connection handle, connect to data source, and allocate statement handle.
- void direxec::sqlconn() {
- SQLAllocEnv(&henv);
- SQLAllocConnect(henv, &hdbc);
- rc = SQLConnect(hdbc, chr_ds_name, SQL_NTS, NULL, 0, NULL, 0);
- // Deallocate handles, display error message, and exit.
- if (!MYSQLSUCCESS(rc)) {
- SQLFreeConnect(henv);
- SQLFreeEnv(henv);
- SQLFreeConnect(hdbc);
- if (hstmt)
- error_out();
- exit(-1);
- }
- rc = SQLAllocStmt(hdbc, &hstmt);
- }
- // Execute SQL command with SQLExecDirect() ODBC API.
- void direxec::sqlexec() {
- SQLLEN mAffectCount;
- SQLSMALLINT mColCount;
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (1, 'HeFei', 'IDN', 'AnHui', 10001)", SQL_NTS);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (2, 'BengBu', 'IDN', 'AnHui', 10002)", SQL_NTS);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO city values (3, 'SuZhou', 'IDN', 'AnHui', 10003)", SQL_NTS);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"UPDATE city SET Name = 'HuaiNan' WHERE Name = 'HeFei'", SQL_NTS);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"DELETE FROM city WHERE Name = 'HuaiNan'", SQL_NTS);
- SQLRowCount(hstmt,&mAffectCount);
- SQLNumResultCols(hstmt,&mColCount);
- SQLCloseCursor(hstmt);
- SQLFreeStmt(hstmt, SQL_UNBIND);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"SELECT * FROM city", SQL_NTS);
- SQLRowCount(hstmt,&mAffectCount);
- SQLNumResultCols(hstmt,&mColCount);
- int ColIndex;
- char Col[100][2048] = {0};
- SQLCHAR ColName[100][2048] = {0};
- SQLLEN mCollocate[100] = {0};
- for (ColIndex=0;ColIndex<mColCount;ColIndex++)
- {
- SQLBindCol(hstmt,ColIndex+1,SQL_C_CHAR,Col[ColIndex],2048,&mCollocate[ColIndex]);
- SQLDescribeCol(hstmt,ColIndex+1,ColName[ColIndex],30,NULL,NULL,NULL,NULL,NULL);
- }
- puts("SQLFetch");
- while ((rc = SQLFetch(hstmt)) != SQL_NO_DATA)
- {
- printf("%s %s %s %s %s\n", Col[0], Col[1], Col[2], Col[3], Col[4]);
- }
- SQLCloseCursor(hstmt);
- SQLFreeStmt(hstmt, SQL_UNBIND);
- rc = SQLExecDirect(hstmt, (SQLCHAR*)"SELECT * FROM city", SQL_NTS);
- SQLRowCount(hstmt,&mAffectCount);
- SQLNumResultCols(hstmt,&mColCount);
- puts("\nSQLGetData");
- memset(Col, 0, 100*2048*sizeof(char));
- for (int RowIndex=0;RowIndex<mAffectCount;RowIndex++)
- {
- SQLFetch(hstmt);
- for (int ColIndex=0;ColIndex<mColCount;ColIndex++)
- {
- int TotalGet = 0;
- SQLLEN LenData = 0;
- int Ret = 0;
- while(Ret=SQLGetData(hstmt, ColIndex+1, SQL_C_CHAR,Col[ColIndex]+TotalGet,200000,&LenData)!=SQL_NO_DATA)
- {
- break;
- }
- }
- printf("%s %s %s %s %s\n", Col[0], Col[1], Col[2], Col[3], Col[4]);
- }
- }
- // Free the statement handle, disconnect, free the connection handle, and free the environment handle.
- void direxec::sqldisconn() {
- SQLFreeStmt(hstmt,SQL_DROP);
- SQLDisconnect(hdbc);
- SQLFreeConnect(hdbc);
- SQLFreeEnv(henv);
- }
- // Display error message in a message box that has an OK button.
- void direxec::error_out() {
- unsigned char szSQLSTATE[10];
- SDWORD nErr;
- unsigned char msg[SQL_MAX_MESSAGE_LENGTH + 1];
- SWORD cbmsg;
- while (SQLError(0, 0, hstmt, szSQLSTATE, &nErr, msg, sizeof(msg), &cbmsg) == SQL_SUCCESS) {
- sprintf_s((char *)szData, sizeof(szData), "Error:\nSQLSTATE=%s, Native error=%ld, msg='%s'", szSQLSTATE, nErr, msg);
- MessageBox(NULL, (const char *)szData, "ODBC Error", MB_OK);
- }
- }
- int main () {
- direxec x; // Declare an instance of the direxec object.
- x.sqlconn(); // Allocate handles, and connect.
- x.sqlexec(); // Execute SQL command
- x.sqldisconn(); // Free handles and disconnect
- getchar();
- }
复制代码
|
|