C++ otlv4 连接 sql server 数据库小记
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 数据库小记的更多相关文章
- python 使用pymssql连接sql server数据库
python 使用pymssql连接sql server数据库 #coding=utf-8 #!/usr/bin/env python#------------------------------ ...
- NetBeans连接SQL server数据库教程
不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...
- 【转】PowerShell 连接SQL Server 数据库 - ADO.NET
转至:http://www.pstips.net/connect-sql-database.html PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本.工作中整 ...
- JDBC连接sql server数据库及其它
JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的 ...
- ThinkPHP连接sql server数据库
亲身经历,在网上找连接sql server数据库的方法,还是不好找的,大多数都是照抄一个人的,而这个人的又写的不全,呵呵,先介绍一下我连接的方法吧.如果你是用THINKPHP连接,那么最重要的就是配置 ...
- JDBC连接sql server数据库的详细步骤和代码
JDBC连接sql server数据库的详细步骤和代码 JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Ja ...
- python连接sql server数据库实现增删改查
简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...
- 详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作
总结一下,连接SQL Server数据库需要以下几个步骤: 1. 导入驱动Jar包:sqljdbc.jar 2. 加载并注册驱动程序 3. 设置连接路径 4. 加载并注册驱动 5. 连接数据库 6. ...
- Windows 2008服务器环境PHP连接SQL Server数据库的配置及连接方法
背景: PHP程序常用的数据库是Mysql数据库,但是由于实际项目需要,要求PHP网站连接SQL Server数据库查询一些必要信息.因此,本文就来给大家介绍一下如何安装及配置PHP扩展,可以实现PH ...
随机推荐
- andriod webview和h5
1.WebBrowserActivity extends BaseActivity 2.setContentView(R.layout.activity_web_html); <WebView ...
- MFC常用宏
MFC调试宏 TRACE() 其形式与函数printf()的参数一样,功能是在调试运行时把表达式的值输出到Output调试窗口. Debug版有效 ASSERT()——断言宏, 表达式为真,则程 ...
- EOCS跨链核心技术内幕
EOCS跨链技术的核心就是ICP模块,ICP即Inter Chain Protocol(跨链交互协议),下面着重介绍ICP工作原理和实现细节. Inter Chain Protocol(ICP) IC ...
- @Html.LabelFor 如何直接添加CSS样式
样式用的是bootstrap. 我想单独调整一下其中一个控件的样式,大概这个造型. @Html.LabelFor(m => m.DerivationRate, new { @class = &q ...
- L2-013 红色警报 (25 分) (并查集)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 题目: 战争中保持各个城市间的连通性非 ...
- [吐槽]webpack4
https://webpack.js.org/guides/tree-shaking/ https://www.webpackjs.com/guides/ 插件都过时被替代了,中文文档也没更新过来,坑 ...
- springboo+nginx测试反向代理02
本节对nginx配置方面会略微研究~~ 1:切换到 /opt/nginx-1.8.1/conf 目录,将nginx.conf文件拷贝到 /myprojects/nginx 目录下 2:切换到/opt/ ...
- gardner 算法matlab实现
% 仿真4比特原始数据与星座图的编码映射过程: % 完成16QAM信号的调制解调: % 基带信号符号速率 ps =1Mbps: % 成形滤波器的滚降因子 a=0.8: % 载波信号频率fc=2MHz ...
- VS2017 提示警告 IDE0006
MS的错误帮助:https://github.com/dotnet/roslyn/wiki/Diagnosing-Project-System-Build-Errors 看起来好复杂- 其实嘛- 检查 ...
- 【转】Android开发规范
转自:https://github.com/Blankj/AndroidStandardDevelop 摘要 1 前言 2 AS 规范 3 命名规范 4 代码样式规范 5 资源文件规范 6 版本统一规 ...