简体   繁体   中英

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. 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. 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. Just specify a valid ODBC driver like @user144098 suggested.

However, the SQL Server ODBC driver is a legacy driver that ships with Windows and is intended only for backwards compatibility with legacy apps. Use a modern driver for new development so you can use newer features and TLS 1.2 protocol security. The latest SQL Server ODBC driver as of this writing is 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;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM