ODBC连接到sql server数据库c++
ODBC connection to sql server database c++
我有连接到sql server数据库的问题。我的代码如下所示。它是在visual studio c++中编写的控制台应用程序。程序在打印"连接失败"一行后关闭。连接字符串是否正确?请建议。
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Program had started.." << endl;
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
SQLSMALLINT columns;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
SQLRETURN SR;
char szDSN[] = "test";
char szUID[] = "Admin";
char szAuthStr[] = "password";
cout << "Attempting Connection " << endl;
SR = SQLConnect(dbc, (SQLWCHAR*)szDSN, SQL_NTS, (SQLWCHAR*)szUID, SQL_NTS, (SQLWCHAR*)szAuthStr, SQL_NTS);
cout << "Connecting ... " << endl;
if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
{
cout << "fail to connect" << endl;
}
else
{
cout << "connected" << endl;
}
return 0;
}
你的例子
修复你的例子,并修改它使用一个DSN-less connection
。在linux (fedora 32)下使用g++ code.cpp -std=c++17 -lodbc -o test.out
测试。
// https://stackoverflow.com/questions/39263791/odbc-connection-to-sql-server-database-c
// https://www.easysoft.com/developer/languages/c/odbc_tutorial.html
#include <sql.h>
#include <sqlext.h>
#include <iostream>
#include <string>
using namespace std;
void extract_error(
string fn,
SQLHANDLE handle,
SQLSMALLINT type)
{
SQLINTEGER i = 0;
SQLINTEGER native;
SQLCHAR state[ 7 ];
SQLCHAR text[256];
SQLSMALLINT len;
SQLRETURN ret;
cout << "nThe driver reported the following diagnostics whilst running " << fn << "nn";
do
{
ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
sizeof(text), &len );
if (SQL_SUCCEEDED(ret))
printf("%s:%ld:%ld:%sn", state, i, native, text);
}
while( ret == SQL_SUCCESS );
}
int main()
{
cout << "Program had started.." << endl;
SQLHENV env;
SQLHDBC dbc;
SQLRETURN ret;
SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
SQLRETURN SR;
cout << "Attempting Connection " << endl;
SQLCHAR sqlConnectionString [] = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1433;UID=SA;PWD=yourPassword;";
ret = SQLDriverConnect(dbc, NULL, sqlConnectionString, SQL_NTS,
outstr, sizeof(outstr), &outstrlen,
SQL_DRIVER_NOPROMPT);
cout << "Connecting ... " << endl;
extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
{
cout << "fail to connect" << endl;
}
else
{
cout << "connected" << endl;
}
return 0;
}
Program had started..
Attempting Connection
Connecting ...
The driver reported the following diagnostics whilst running SQLDriverConnect
01000:1:5701:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed database context to 'master'.
01000:2:5703:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed language setting to us_english.
connected
相关文章:
- 查询SQLite数据库中的日期
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在ArangoDb AQL查询中指定数据库
- 从数据库实时显示QT c++中的数据
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 无法在C++中建立与MySQL数据库的连接
- 将类实现到数据库程序中
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 调试编译的服务器在数据库打开时崩溃
- C++变量mysql_query到数据库
- LMDB:在有限的内存系统中打开大型数据库
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 以只读模式打开数据库时SQLITE_CANTOPEN错误
- 如何使用SQLite将数据库中的值导出为C / C++中的字符串或字符?
- 如何在Qt中从数据库中检索二进制数据?
- 如何从网站获取数据并将其传输到数据库?
- 在装有 CLion 的 Mac 上使用 C++ 连接到 MySQL 数据库时出现问题
- Qt5 从 MySQL 数据库中选择数据
- 当我不需要数据库中的所有值时,如何部分初始化 c++ 对象?