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. selenium+webservice进行百度登录

    from selenium import webdriverimport time driver = webdriver.Chrome()driver.get("https://www.ba ...

  2. 学习itop4412开发板有哪些资料可学习?能否学会

    1.光盘资料 下面简单的做一下了解,在需要使用这些资料的时候,会针对性的做详细介绍. 将文件以及文件夹按照“名称+递增”的方式排列,如下: 01_PCB_SCH_DATASHEET-- -----开发 ...

  3. 20175204 张湲祯 2018-2019-2《Java程序设计》2

    20175204 张湲祯 2018-2019-2<Java程序设计>2 必做课下作业MyCP 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP ...

  4. json中的json.dumps()

    Json简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  5. 【easy】268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. PHP常用方法整理

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

  7. hibernate 一对多 取多方数据重复问题,FetchMode.JOIN、FetchMode.SELECT、FetchMode.SUBSELECT区别

    问题描述:稿件附件表数据时出现多条重复数据. 介绍: 表:稿件实体Manuscripts (数据库表MANUSCRIPTS),稿件附件实体ManuscriptsAtt(表MANUSCRIPTS_ATT ...

  8. Python爬虫基础之BeautifulSoup

    一.BeautifulSoup的基本使用 from bs4 import BeautifulSoup from bs4 import SoupStrainer import re html_doc = ...

  9. springboo+nginx测试反向代理01

    操作环境:centos7,springboot2.1,nginx1.8.1 boot程序链接地址 : https://github.com/zgq7/nginxDemo nginx下载地址: http ...

  10. echarts-饼状图默认选中高亮

    1.首页需要设置legend legend: { data: ["积极", "负面"], selectedMode: false, show: false } ...