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. SqlServer如何获取存储过程的返回值

    1.Output参数返回值 1 CREATE PROCEDURE [dbo].[upInformation]( 2 @age int , 3 @id bigint OUTPUT 4 ) 5 AS 6 ...

  2. Python核心编程(网络编程)

    1.python socket模块内置方法 2.tcp服务器伪代码 3.tcp客户端伪代码 4.socket模块属性 5.一个简单的tcp客户端和服务端 服务端代码: # encoding:utf-8 ...

  3. JAVA进阶22

    1.接口默认方法的使用 ①接口的默认方法可以通过接口实现类对象直接调用. ②接口的默认方法也可以被接口实现类进行覆盖重写 package cn.intcast.demo17; public inter ...

  4. js下载后台返回的docx(返回格式:文档流)文件

    原文地址: https://www.jianshu.com/p/a81c68c15fbd PS需要指定responseType类型,不然文件内容会乱码哦 咦?文件名乱码?需要手动设置文件名哦↓ 呀,文 ...

  5. 清北学堂学习总结day3

    小学知识总结 上午篇 •积性函数的卷积公式 (1)(f * g)( n ) = ∑(d|n) f( d ) x g ( n / d ) (2)代码实现 LL f[N], g[N], h[N]; voi ...

  6. springboot启动的时候排除加载某些bean

    由于公司把redis相关的配置类,工具类放在了一个类似common的工程里,这样以后肯定不可避免的出现某些项目可能并不需要使用redis,但是还是依赖common里的别的一些类库 所以排除spring ...

  7. P5302 [GXOI/GZOI2019]特技飞行

    题目地址:P5302 [GXOI/GZOI2019]特技飞行 这里是官方题解(by lydrainbowcat) 题意 给 \(10^5\) 条直线,给 \(x = st\) 和 \(x = ed\) ...

  8. 编译树莓派2代B型OpenWrt固件实现无线路由器及nodogsplash认证功能

    最终功能: 无线路由器的主要功能,网口WAN接入,USB无线网卡AP热点输出,连上wifi之后跳转到认证页面,点击认证方可上网,有效时间10分钟,认证成功之后自动访问指定网址. 文章结尾有编译好的刷机 ...

  9. .Net业务搭配实用技术栈(转)

      前言 昨天有篇文章在讨论webform的设计思路,我已经四五年不用webform了,虽然它也提供了HttpModule和httphandle来处理请求,提供了一般处理程序ashx来简化处理流程,但 ...

  10. JMM以及并发三大特性介绍(包括解决方案)

    JMM结构图: JMM对同步的8种操作: JMM的同步规则: Countdownlatch介绍: 该类功能是可以阻塞线程,并在保证线程满足特定条件下,继续执行.如上图,Countdownlatch的c ...