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. Linux系统GNOME主题安装与Tweaks工具使用

    需要软件: GNOME Tweaks--使主题修改更加容易一个工具 安装主题: 下载主题:mac themes下载链接:https://www.gnome-look.org/p/1241688/ 这里 ...

  2. 如何用ps简单快速扣头发丝

    好久不用PS抠图,今天接到一个小任务,换背景,以前一直用通道的办法,但用通道比较费劲,发现一个更简单的办法,就是用快速蒙版+调整边缘. 这张是原图: 1.先用快速蒙版制作选取(Q) 再按Q,退出快速蒙 ...

  3. word20170106在机场 At the airport有用的词和句子

    有用的词: airport terminal: 航站楼 domestic flight: 国内航班 international flight: 国际航班 checked luggage: 托运行李 c ...

  4. pyQt5不让进度条卡住

    这里我们用一个更新程序做示例, 下载文件的过程中让进度条实时显示下载进度. 如果下载和更新进度条的工作都放在一个线程中,会出现进度条卡顿的情况. Qt中正确的做法是把界面刷新和工作任务交给不同的线程去 ...

  5. [系统集成] 基于 elasticsearch 的企业监控方案

    注: 2017年10月16日: 使用中发现 es 查询时序数据的性能较差,且 watch 脚本的编写比较麻烦,因此已将监控系统切换到了 influxdb+grafana平台.新监控系统各方面情况比较满 ...

  6. 使用Lottie将AE项目转换为 Web 原生动画

    使用Lottie转换AE项目为 Web 原生动画 首先打开链接https://github.com/airbnb/lottie-web/blob/master/build/extension/body ...

  7. # 20175333曹雅坤《Java程序设计》第2周学习总结

    教材学习内容总结 1.学习第二,三章ppt,并观看视频. 2.在虚拟机中连接到码云,克隆代码,编译与运行教材上的例子. 3.在虚拟机上安装相关配置,使其满足学习要求. 4.运行并截图上传监督学习脚本s ...

  8. 基于用户的协同过滤电影推荐user-CF python

    协同过滤包括基于物品的协同过滤和基于用户的协同过滤,本文基于电影评分数据做基于用户的推荐 主要做三个部分:1.读取数据:2.构建用户与用户的相似度矩阵:3.进行推荐: 查看数据u.data 主要用到前 ...

  9. JAVA Scanner的简单运用

    package Code428; import java.util.Scanner; /*Scanner 可以实现键盘输入数据 引用的步骤1.import 包路径.类名称只有java.lang包下的内 ...

  10. iOS开发之获取时间戳方法

    // 得到当前本地时间,13位,整形 + (long long)gs_getCurrentTimeToMilliSecond { double currentTime = [[NSDate date] ...