我在Windows 10操作系统的PC上运行Visual Studio 2021。
我想用c++应用程序连接到mssql服务器。我确实尝试了许多不同的连接字符串,但我不知道为什么我不能连接到服务器。我仍然得到以下输出:Could not connect to SQL Server.
下面的代码:
void configDatabase() {
#define SQL_RESULT_LEN 240
#define SQL_RETURN_CODE_LEN 3000
//define handles and variables
SQLHANDLE sqlConnHandle;
SQLHANDLE sqlStmtHandle;
SQLHANDLE sqlEnvHandle;
SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
//initializations
sqlConnHandle = NULL;
sqlStmtHandle = NULL;
//allocations
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle)) {
goto COMPLETED;
}
if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) {
goto COMPLETED;
}
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle)) {
goto COMPLETED;
}
//output
cout << "Attempting connection to SQL Server...";
cout << "n";
//connect to SQL Server
switch (SQLDriverConnect(sqlConnHandle, NULL,
(SQLWCHAR*)TEXT("DRIVER={SQL Server};SERVER=LAPTOP-KD0SFV4F\MSSQLSERVER1,1433;DATABASE=dbFotfChallenge;UID=Tom;PWD=Tom;Trusted_Connection=no;"),
SQL_NTS, retconstring, 1024, NULL, SQL_DRIVER_NOPROMPT)) {
case SQL_SUCCESS:
cout << "Successfully connected to SQL Server";
cout << "n";
break;
case SQL_SUCCESS_WITH_INFO:
cout << "Successfully connected to SQL Server";
cout << "n";
break;
case SQL_INVALID_HANDLE:
cout << "Could not connect to SQL Server";
cout << "n";
goto COMPLETED;
case SQL_ERROR:
cout << "Could not connect to SQL Server";
cout << "n";
goto COMPLETED;
default:
break;
}
//if there is a problem connecting then exit application
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle))
goto COMPLETED;
//output
cout << "n";
cout << "Executing T-SQL query...";
cout << "n";
//if there is a problem executing the query then exit application
//else display query result
if (SQL_SUCCESS != SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) {
cout << "Error querying SQL Server";
cout << "n";
goto COMPLETED;
}
else {
//declare output variable and pointer
SQLCHAR sqlVersion[SQL_RESULT_LEN];
SQLINTEGER ptrSqlVersion;
while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
//display query result
cout << "nQuery Result:nn";
cout << sqlVersion << endl;
}
}
//close connection and free resources
COMPLETED:
SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
SQLDisconnect(sqlConnHandle);
SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);
//pause the console window - exit when key is pressed
cout << "nPress any key to exit...";
getchar();
将Trusted_Connection
设置为no
,而不是false
。
no
不是一个有效值。