otlv4介绍:

http://otl.sourceforge.net/

测试代码

// testotlv4.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
using namespace std; #include <stdio.h>
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000
#include "otlv4.h" // include the OTL 4.0 header file otl_connect db; // connect object void insert()
// insert rows into table
{
otl_stream o(10, // buffer size
"insert into test_tab values(:f1<int>,:f2<char[31]>)",
// SQL statement
db // connect object
); o.set_commit(0); // set stream's auto-commit to OFF. try {
o << 1 << "Line1"; // Enter one row into the stream
o.flush(); // flush the strem buffer, i.e. force
// the stream to execute
#if defined(OTL_ANSI_CPP_11_VARIADIC_TEMPLATES)
otl_write_row(o, 1, "Line1");
#else
// when variadic template functions are not supported by the C++
// compiler, OTL provides nonvariadic versions of the same template
// functions in the range of [1..15] parameters
otl_write_row(o, 1, "Line1"); // the old way (operators >>() / <<()) is available as always:
// o<<1<<"Line1"; // Enter the same data into the stream
// and cause a "duplicate key" error.
#endif
o.flush();
}
catch (otl_exception& p) {
if (p.code == 2601) {
// ... duplicate key ...
cout << "STREAM ERROR STATE=" << o.get_error_state() << endl;
o.clean(1); // clean up the stream's buffer
// and clean up the stream's internal
// error flag as well. By doing this,
// it's possible to recover from
// a database error without closing
// the stream. Remember, the number of
// situtation when it's possible is
// limited and the recovery procedure should
// be carefully designed.
}
else
throw; // re-throw the exception to the outer catch block.
} o << 2 << "Line2"; // Enter one more row of data after
// recovering from the "duplicate key"
// error
o.flush(); db.commit(); // commit transaction } void select()
{
otl_stream i(10, // buffer size
"select * from test_tab",
// SELECT statement
db // connect object
);
// create select stream int f1;
char f2[31]; #if (defined(_MSC_VER) && _MSC_VER>=1600) || defined(OTL_CPP_11_ON)
// C++11 (or higher) compiler
for (auto& it : i) {
#if defined(OTL_ANSI_CPP_11_VARIADIC_TEMPLATES)
otl_read_row(it, f1, f2);
#else
// when variadic template functions are not supported by the C++
// compiler, OTL provides nonvariadic versions of the same template
// functions in the range of [1..15] parameters
otl_read_row(it, f1, f2); // the old way (operators >>() / <<()) is available as always:
// it>>f1>>f2;
#endif
cout << "f1=" << f1 << ", f2=" << f2 << endl;
}
#else
// C++98/03 compiler
while (!i.eof()) { // while not end-of-data
i >> f1 >> f2;
cout << "f1=" << f1 << ", f2=" << f2 << endl;
}
#endif } int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try { // CString strCon;
//strCon.Format("driver=sql server;server=%s;UID=%s;PWD=%s;database=%s",
// strServerIP.c_str(), strServerUID.c_str(), strServerPWD.c_str(), strServerDatabase.c_str());
string strCon1 = "DSN=testsqlserver;UID=sa;PWD=12345678;database=aa_note";
//strCon1 = "driver=sql server;server=127.0.0.1;UID=sa;PWD=12345678;database=aa";
db.rlogon(strCon1.c_str()); // connect to the database 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))"
); // create table otl_cursor::direct_exec
(
db,
"create unique index ind001 on test_tab(f1)"
); // create unique index insert(); // insert records into table
select(); // select records from table } catch (otl_exception& p) { // intercept OTL exceptions
cout << p.code << endl; // print out error code
cout << p.sqlstate << endl; // print out error SQLSTATE
cout << p.msg << endl; // print out error message
cout << p.stm_text << endl; // print out SQL that caused the error
cout << p.var_info << endl; // print out the variable that caused the error
} db.logoff(); // disconnect from the database return 0; }

  

string strCon1 = "DSN=testsqlserver;UID=sa;PWD=12345678;database=aa_note";
//strCon1 = "driver=sql server;server=127.0.0.1;UID=sa;PWD=12345678;database=aa";
db.rlogon(strCon1.c_str()); // connect to the database

有3种连接方式 ,

1,数据源odbc连接

这儿不选择,后面通过 程序来选择.

2,代码直接 连接 "driver=sql server;server=127.0.0.1;UID=用户名;PWD=密码;database=数据库";

3,USER/PASSWORD@TNS_ALIAS 这种方式  要注意 数据库密码有@时会报错

C++ otlv4 连接 sql server 数据库小记的更多相关文章

  1. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  2. NetBeans连接SQL server数据库教程

    不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...

  3. 【转】PowerShell 连接SQL Server 数据库 - ADO.NET

    转至:http://www.pstips.net/connect-sql-database.html PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本.工作中整 ...

  4. JDBC连接sql server数据库及其它

    JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的 ...

  5. ThinkPHP连接sql server数据库

    亲身经历,在网上找连接sql server数据库的方法,还是不好找的,大多数都是照抄一个人的,而这个人的又写的不全,呵呵,先介绍一下我连接的方法吧.如果你是用THINKPHP连接,那么最重要的就是配置 ...

  6. JDBC连接sql server数据库的详细步骤和代码

    JDBC连接sql server数据库的详细步骤和代码 JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Ja ...

  7. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  8. 详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作

    总结一下,连接SQL Server数据库需要以下几个步骤: 1. 导入驱动Jar包:sqljdbc.jar 2. 加载并注册驱动程序 3. 设置连接路径 4. 加载并注册驱动 5. 连接数据库 6. ...

  9. Windows 2008服务器环境PHP连接SQL Server数据库的配置及连接方法

    背景: PHP程序常用的数据库是Mysql数据库,但是由于实际项目需要,要求PHP网站连接SQL Server数据库查询一些必要信息.因此,本文就来给大家介绍一下如何安装及配置PHP扩展,可以实现PH ...

随机推荐

  1. String构造函数

    只简单写了几个函数 class String { public: String(const char* pStr = NULL); String(const String& str); vir ...

  2. JavaFX - 富互联网应用

    JavaFX教程™ --必看https://www.yiibai.com/javafx /================= 富互联网应用 是那些提供与Web应用程序类似的功能,并可作为桌面应用程序体 ...

  3. selenium模块

    一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被夹在 元素交互操作 其他 项目联 一 介绍 selenium最初是一个自动化测试的工具,而爬虫中使用它主要是为了解决requests无法直接 ...

  4. PHP常用方法整理

    最近开始写PHP项目,各种常用的方法简单整理一下,以备后用. 1.  Xml转Json json_decode(json_encode(simplexml_load_string($xml, 'Sim ...

  5. nvm的使用

    nvm能下载并按照指定 的版本,还能切换已安装好的版本,相当的好用

  6. Lua模式匹配

    Lua并不使用POSIX规范的正则表达式[4](也写作regexp)来进行模式匹配.主要的原因出于程序大小方面的考虑:实现一个典型的符合POSIX标准的regexp大概需要4000行代码,这比整个Lu ...

  7. redis哨兵主从自动切换

    1.用的是TP5框架,改写框架自带的redis类 thinkphp/library/think/cache/driver/Redis.php //两台服务器都配置好了监控哨兵 //主从配置要设置好密码 ...

  8. ThinkPHP 2053错误

    这个报错是调用存储过程的时候产生的,用的是5.1的代码是根据官方文档写的,我怀疑5.0也有这个问题.去官方查了一下发现不少人有这个问题,但是官方都没有回应过,只能自己动手一步步调了. $center ...

  9. 一次String的错误使用

    今日,在写代码的过程中,程序执行一个方法居然出现了heap space溢出的异常. 从来还没有遇到这样的异常,打断点发现是做字符串拼接时出现此异常. 所以知道了是String使用的异常,因为我做的操作 ...

  10. Spring Session产生的sessionid与cookies中的sessionid不一样的问题 && httpOnly 设置不起作用的问题??

    背景: Springboot 2.0 (spring-session-data-redis + spring-boot-starter-web) 需求: 通过cookies中取到的 sessionid ...