Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- DockerWindows
- .netCore Install
- 도커마운트
- Ubuntu VirtualBox
- ubuntu .net Core
- VirtualBox Ubuntu
- .dotnet Core 3.1 Install
- Winsock
- wol
- .netCore3.1
- Ubuntu Server VirtualBox
- DockerVolume
- 우분투서버가상머신
- 닷넷코어
- Volume Mount
- 윈도우10 Wake On Lan
- IOCP
- dotnet Core
- 윈도우10 WOL
- mongo docker
- mongodb readonly
- Docker오류
- IOCP 서버
- DockerDesktop
- mongodb pss
- docker desktop
- docker
- MySQL
- IOCP 클라이언트
- Wake On Lan
Archives
- Today
- Total
;
C언어 ODBC를 이용하여 특정 아이디의 데이터 찾아 뿌려주기 본문
반응형
// 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 쿼리문을 보내준다.
반응형
'C, C++, C#' 카테고리의 다른 글
C# HttpWebRequest 웹 파싱하기 (0) | 2017.06.28 |
---|---|
C# 특정글자 파싱하기 SplitParsing (2) | 2017.06.28 |
IOCP 구조체 전송 & 구조체 받기 (0) | 2016.12.04 |
IOCP Server[에코서버], Client (0) | 2016.12.03 |
Winsock 구조체 데이터 보내고 받기 (1) | 2016.12.01 |
Comments