#pragma once
#define OTL_ODBC_MSSQL_2005
#define OTL_STL
#define OTL_UNICODE
#define OTL_UNICODE_EXCEPTION_AND_RLOGON
#include <otl/otlv4.h>
// DB 준비
/*
-- CREATE TEST INSERT TABLE
CREATE TABLE LogData
(
errormessage VARCHAR(1024)
)
-- CREATE STORED PROCEDURE
CREATE procedure [dbo].[my_proc]
@A int out,
@B varchar(60) out,
@C varchar(60)
as
SET NOCOUNT ON
begin
select @A=@A+1
select @B=@C
return 111
end
-- TEST STORED PROCEDURE
Declare @rc INT
Declare @b VARCHAR(60)
exec @rc = my_proc 1, @b output, 'cccc'
select @rc
select @b
*/
class TestOtl
{
public:
TestOtl(){}
~TestOtl(){}
void insert(otl_connect& db)
{
otl_stream o;
o.open(100, "insert into LogData values(:f1<char[1024]>)", db);
o.set_commit(0); // ROLLBACK // auto commmit off
std::wstring errormsg = L"ERROR 한글 MESSAGE";
for (int i=0; i<2; ++i) {
o << errormsg.c_str();
}
o.set_flush(); // ROLLBACK // not yet commited
}
void select(otl_connect& db)
{
otl_stream i;
i.open(10, "select * from LogData", db);
wchar_t errormsg[1024] = {0};
while (!i.eof()) {
i >> errormsg;
wcout << L"errormsg : " << errormsg << endl;
}
i.clean();
}
void storedProcedure(otl_connect& db)
{
otl_stream o(1, "{:rc<int,out> = call my_proc( :A<int,inout>, :B<char[31],out>, :C<char[31],in> )}", db);
o.set_commit(0); // set stream auto-commit off since the stream does not generate transaction
o << 1 << L"Test String1"; // assigning :1 = 1, :3 = "Test String1"
int a;
wchar_t b[31] = {0};
int result = -1;
o >> result; // return
o >> a >> b;
wcout << L"A=" << a << L", B="<< b << endl;
wcout << L"result : " << result << endl;
}
void main()
{
int multithread_mode = 1; // 0 : single-thread, 1 : multi-thread
int tret1 = otl_connect::otl_initialize(multithread_mode);
try {
otl_connect db; // connect object;
//config
int timeout = 5; // sec. sql statement timeout
db.set_timeout(timeout);
// @가 들어있는 경우 \\ 처리 필요 : AAA@a -> AAA\\@a
std::string connection_string = "DRIVER={SQL Server};SERVER=192.168.56.101,1433;DATABASE=Test;UID=sa;PWD=AAA\\@1;";
db.rlogon(connection_string.c_str());
db.auto_commit_off();
db.set_transaction_isolation_level(otl_tran_read_committed); // ROLLBACK
while (true) {
insert(db);
db.rollback(); // ROLLBACK
select(db);
storedProcedure(db);
}
db.logoff(); // disconnect
} catch(otl_exception& e) {
wcout << L"error code : " << e.code << L"\n"
<< L"SQLSTATE : " << e.sqlstate << L"\n"
<< L"error msg : " << e.msg << L"\n"
<< L"error statment : " << e.stm_text << L"\n"
<< L"error variable : " << e.var_info << L"\n";
}
}
};
'C++' 카테고리의 다른 글
ado 하위 OS 호환 컴파일 - E_NOINTERFACE (0x80004002) (0) | 2012.04.13 |
---|---|
MFC, GDI (0) | 2012.04.06 |
WTL : Windows Template Library (0) | 2012.04.06 |
log4cxx warning 관련 C4275 (0) | 2012.04.06 |
현재 자신이 서비스 모드로 실행되었는지 콘솔모드로 실행되었는지 확인하는 방법 (0) | 2012.04.05 |
strsafe.h (0) | 2012.03.24 |
openssl + AES256 + Base64 (0) | 2012.03.11 |
UTF-8 변환 (UTF8Conv.h) (0) | 2012.03.10 |