项目结构
ConsoleApplication.cpp
#include <iostream>
using namespace std;
#include <stdio.h>
#define OTL_ODBC_MSSQL_2008
#include "..\c++_otlv4/otlv4_h2/otlv4.h"
otl_connect db; // connect object
void insert(void)
// insert rows into table
{
otl_stream o(100,
"insert into test_tab values(:f1<int>,:f2<char[31]>,:f3<char[31]>,:f4<char[31]>,:f5<char[31]>)",
db
);
o.set_commit(0); // set stream's auto-commit to OFF.
try {
/* o << 1 << "Line1";
o.flush();
otl_write_row(o, 1, "Line1");
o.flush();*/
for (int i = 1; i <= 100000; ++i) {
o << i << "你爱打不打断打实大" << "2775275" << "fsdfdfs" << "你爱打算打实打实大";
}
}
catch (otl_exception& p) {
if (p.code == 2601) {
// ... duplicate key ...
cout << "STREAM ERROR STATE=" << o.get_error_state() << endl;
o.clean(1);
}
else
throw; // re-throw the exception to the outer catch block.
}
/*o << 2 << "Line2";
o.flush();*/
db.commit(); // commit transaction
}
void select(void)
{
otl_stream i(10, // buffer size
"select * from test_tab",
// SELECT statement
db // connect object
);
// create select stream
int f1;
char f2[31];
for (auto& it : i) {
otl_read_row(it, f1, f2);
cout << "f1=" << f1 << ", f2=" << f2 << endl;
}
}
int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
DWORD start, end; //定义程序运行开始时间和结束时间
start = GetTickCount64(); //设定开始时间
cout << "开始";
try {
db.rlogon("driver=sql server;server=192.168.1.104;UID=sa;PWD=123;database=Test"); // connect to ODBC
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // drop table
otl_cursor::direct_exec
(
db,
"create table test_tab(f1 int, f2 varchar(30), f3 varchar(30), f4 varchar(30), f5 varchar(30))"
); // create table
insert(); // insert records into table
//select(); // select records from table
}
catch (otl_exception& p) { // intercept OTL exceptions
cerr << p.msg << endl; // print out error message
cerr << p.stm_text << endl; // print out SQL that caused the error
cerr << p.sqlstate << endl; // print out SQLSTATE message
cerr << p.var_info << endl; // print out the variable that caused the error
}
db.logoff(); // disconnect from Oracle
end = GetTickCount64();
cout << "use time: " << (end - start)/1000.00<<"秒" << endl;
return 0;
}