;

C언어 ODBC를 이용하여 특정 아이디의 데이터 찾아 뿌려주기 본문

C, C++, C#

C언어 ODBC를 이용하여 특정 아이디의 데이터 찾아 뿌려주기

WindowsHyun 2017. 5. 31. 01:17
반응형
// SQLBindCol_ref.cpp  
// compile with: odbc32.lib  
#include   
#include   

using namespace std;

#define UNICODE  
#include   

#define NAME_LEN 50  
#define PHONE_LEN 20  

void show_error() {
	cout << "Error!\n";
}

int main() {
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt = 0;
	SQLRETURN retcode;
	SQLWCHAR szName[NAME_LEN];
	int x, y;
	char game_id[10];
	SQLLEN cbName = 0, cbID = 0, cbPLevel = 0;

	char  buf[255];
	wchar_t sql_data[255];
	SQLWCHAR sqldata[255] = { 0 };

	// Allocate environment handle  
	retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv );

	// Set the ODBC version environment attribute  
	if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
		retcode = SQLSetEnvAttr( henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0 );

		// Allocate connection handle  
		if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
			retcode = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc );

			// Set login timeout to 5 seconds  
			if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
				SQLSetConnectAttr( hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0 );

				// Connect to data source  
				retcode = SQLConnect( hdbc, (SQLWCHAR*)L"ODBC_DB", SQL_NTS, (SQLWCHAR*)NULL, 0, NULL, 0 );

				// Allocate statement handle  
				if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
					retcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt );

					cin >> game_id;
					sprintf( buf, "EXEC dbo.select_id %s",user_id );
					MultiByteToWideChar( CP_UTF8, 0, buf, strlen( buf ), sql_data, sizeof sql_data / sizeof *sql_data );
					sql_data[strlen( buf )] = '\0';

					retcode = SQLExecDirect( hstmt, sql_data, SQL_NTS );
					if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {

						// Bind columns 1, 2, and 3  
						retcode = SQLBindCol( hstmt, 1, SQL_WCHAR, szName, NAME_LEN, &cbName );
						retcode = SQLBindCol( hstmt, 2, SQL_INTEGER, &x, 100, &cbID );
						retcode = SQLBindCol( hstmt, 3, SQL_INTEGER, &y, PHONE_LEN, &cbPLevel );

						// Fetch and print each row of data. On an error, display a message and exit.  
						for ( int i = 0; ; i++ ) {
							retcode = SQLFetch( hstmt );
							if ( retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO )
								show_error();
							if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
								cout << "Record : " << i + 1;
								wcout << " ID : " << szName;
								cout << " x : " << x;
								cout << " y : " << y;
								cout << "\n";
							}
							else
								break;
						}
					}

					// Process data  
					if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO ) {
						SQLCancel( hstmt );
						SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
					}

					SQLDisconnect( hdbc );
				}

				SQLFreeHandle( SQL_HANDLE_DBC, hdbc );
			}
		}
		SQLFreeHandle( SQL_HANDLE_ENV, henv );
	}
}


sprintf를 통하여 각 사용자마다 id가 다르므로 해당 아이디를 buf에 입력을 해준 뒤


MultiByteToWideChar 함수를 이용하여 char인 buf를 wchar형태로 변환을 하여 SQLExecDirect에 sql 쿼리문을 보내준다.



반응형
Comments