![](/img/trans.png)
[英][Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[英]SQL Server - data source name not found and no default driver specified
我正在嘗試使用C ++連接SQL Server,但是即使添加了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協議安全性。 撰寫本文時,最新的SQL Server ODBC驅動程序是SQL Server的ODBC驅動ODBC Driver 17 for SQL Server
。
安裝驅動程序后,如下更改連接字符串。 我相信實例名稱之前的反斜杠需要在字符串文字中進行轉義,因此我將斜杠加倍。
"DRIVER={ODBC Driver 17 for SQL Server};Server=hostname\\SQLEXPRESS;Database=master;Trusted_Connection=True;"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.