c++ - SQL Server - 未找到数据源名称且未指定默认驱动程序

标签 c++ sql-server

我正在尝试使用 C++ 连接 SQL 服务器,但即使在添加 DSN 后仍收到错误“未找到数据源名称且未指定默认驱动程序”。我有一台 win 10 64 位机器,我在系统 DSN 下为 32 位和 64 位添加了同名的 DSN。请帮助我也获得正确的连接字符串。

下面是我正在尝试做的。

#include <stdafx.h>
#include <windows.h>  
#include <sqlext.h>  
#include <iostream>
#include <locale>
#include <codecvt>

using namespace std;

void show_error(unsigned int handletype, const SQLHANDLE& handle)
{
    SQLWCHAR sqlstate[1024];
    SQLWCHAR message[1024];

    if (SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
    {
        std::wstring wMsg(message);
        std::wstring wState(sqlstate);

        //setup converter
        using convert_type = std::codecvt_utf8<wchar_t>;
        std::wstring_convert<convert_type, wchar_t> converter;

        //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr)
        std::string converted_msg = converter.to_bytes(wMsg);
        std::string converted_state = converter.to_bytes(wState);


        std::cout << "Message: " << converted_msg/*message*/ << "\nSQLSTATE: " << converted_state/*sqlstate*/ << std::endl;
    }
}

int main() {
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt;
    SQLRETURN retcode;

    SQLWCHAR OutConnStr[255];
    SQLSMALLINT OutConnStrLen;

    HWND desktopHandle = GetDesktopWindow();   // desktop's window handle  

                                               // Allocate environment handle  
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

    // Set the ODBC version environment attribute  
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
        retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0);

        // Allocate connection handle  
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

            // Set login timeout to 5 seconds  
            if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)50, 0);

                SQLWCHAR retconstring[1024];
                retcode = SQLDriverConnect(hdbc, NULL,
                                (SQLWCHAR*)"DRIVER={SQL1};Server=hostname\SQLEXPRESS;Database=master;Trusted_Connection=True;",
                                SQL_NTS, retconstring, 1024, NULL, SQL_DRIVER_NOPROMPT);

                // Allocate statement handle  
                if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

                    // Process data  
                    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
                        SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                    }

                    SQLDisconnect(hdbc);
                }
                else
                    show_error(SQL_HANDLE_DBC, hdbc);

                SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
            }
        }
        SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }
}

最佳答案

由于您在连接字符串中指定了驱动程序而不是 DSN,因此根本不需要创建 DSN。只需指定一个有效的 ODBC 驱动程序,如建议的 @user144098。

但是,SQL Server ODBC 驱动程序是 Windows 附带的旧版驱动程序,仅用于与旧版应用程序向后兼容。使用现代驱动程序进行新开发,以便您可以使用更新的功能和 TLS 1.2 协议(protocol)安全性。截至撰写本文时,最新的 SQL Server ODBC 驱动程序是 ODBC Driver 17 for SQL Server .

安装驱动程序后,如下更改连接字符串。我认为实例名称前的反斜杠需要在字符串文字中转义,所以我将斜杠加倍。

"DRIVER={ODBC Driver 17 for SQL Server};Server=hostname\\SQLEXPRESS;Database=master;Trusted_Connection=True;"

关于c++ - SQL Server - 未找到数据源名称且未指定默认驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291624/

相关文章:

c# - 是否有理由在 c# 中使用 clausule 检查多个内部的 null?

c++ - 写入共享内存的频率限制?

c++ - 了解虚拟拷贝构造函数

c++ - 如何从 pqxx 调用重载的远程过程

sql - 如何获取最后插入的主键?

sql-server - WinForms 应用程序设计——将文档从 SQL Server 移动到文件存储

sql-server - Coldfusion - 如何解析和分割电子邮件文件中的数据

使用 SQL 插入查询的 Python 3 导致错误 "column count doesn' t 行匹配值计数”

c++ - 置换 vector

c++ - 系统日志不转发远程消息