简体   繁体   English

SQL Server-找不到数据源名称,也未指定默认驱动程序

[英]SQL Server - data source name not found and no default driver specified

I am trying to connect SQL server using C++ but getting error "data source name not found and no default driver specified" even after adding the DSN. 我正在尝试使用C ++连接SQL Server,但是即使添加了DSN,也会出现错误“找不到数据源名称且未指定默认驱动程序”。 I have a win 10 64 bit machine and I have added DSN with same name under system DSN for 32 bit and 64 bit. 我有一台Win 10 64位计算机,并且在系统DSN下为32位和64位添加了具有相同名称的DSN。 Please help me get the correct connection string also. 请也帮助我获得正确的连接字符串。

Below is what I'm trying to do. 以下是我要执行的操作。

#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);
    }
}

Since you've specified a driver instead of DSN in the connection string, there is no need to create a DSN at all. 由于您在连接字符串中指定了驱动程序而不是DSN,因此根本不需要创建DSN。 Just specify a valid ODBC driver like @user144098 suggested. 只需指定一个有效的ODBC驱动程序,如建议的@ user144098。

However, the SQL Server ODBC driver is a legacy driver that ships with Windows and is intended only for backwards compatibility with legacy apps. 但是, SQL Server ODBC驱动程序是Windows附带的旧版驱动程序,仅用于与旧版应用程序向后兼容。 Use a modern driver for new development so you can use newer features and TLS 1.2 protocol security. 使用现代驱动程序进行新开发,以便可以使用更新的功能和TLS 1.2协议安全性。 The latest SQL Server ODBC driver as of this writing is ODBC Driver 17 for SQL Server . 撰写本文时,最新的SQL Server ODBC驱动程序是SQL Server的ODBC驱动ODBC Driver 17 for SQL Server

After installing the driver, change your connection string like below. 安装驱动程序后,如下更改连接字符串。 I believe the backslash before the instance name needs to be escaped in the string literal so I doubled the slash. 我相信实例名称之前的反斜杠需要在字符串文字中进行转义,因此我将斜杠加倍。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [Microsoft] [ODBC驱动程序管理器]找不到数据源名称,未指定默认驱动程序 - [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified Unicode SQLDriverConnectW(): [unixODBC][Driver Manager]未找到数据源名称,且未指定默认驱动程序 - Unicode SQLDriverConnectW(): [unixODBC][Driver Manager]Data source name not found, and no default driver specified 由于系统错误8(SQL Server)错误,无法加载指定的驱动程序 - Specified driver could not be loaded due to system error 8 (SQL Server) error 为Sql server实现odbc包装器。 将数据库数据作为字符读取或要求驱动程序将数据转换为C类型 - Implementing odbc wrapper for Sql server. Reading database data as characters or asking driver to convert the data to C type sql :: mysql :: get_driver_instance()找不到符号 - sql::mysql::get_driver_instance() Symbol not found 是否应在头文件或 .cpp 源文件中指定 C++ 函数默认参数值? - Should C++ function default argument values be specified in headers or .cpp source files? SQL Server的批量复制(C API)和ODBC驱动程序 - Bulk copy (C API) and ODBC driver for SQL Server 找到但未加载MYSQL驱动程序 - MYSQL driver found but not loaded gethostbyname 说没有这样的服务是已知的。 在指定的名称空间中找不到服务 - gethostbyname says No such service is known. The service cannot be found in the specified name space Windows LoadMenu 错误:“在图像文件中找不到指定的资源名称。” - Windows LoadMenu error: “The specified resource name cannot be found in the image file.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM