Reputation: 59
I am having problem with the connection to the sql server database. My code is as shown below. It is written in visual studio c++ console application. The program closes after printing the line "Fail to connect". Is the connection string correct? Please advice.
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Program had started.." << endl;
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
SQLSMALLINT columns;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
SQLRETURN SR;
char szDSN[] = "test";
char szUID[] = "Admin";
char szAuthStr[] = "password";
cout << "Attempting Connection " << endl;
SR = SQLConnect(dbc, (SQLWCHAR*)szDSN, SQL_NTS, (SQLWCHAR*)szUID, SQL_NTS, (SQLWCHAR*)szAuthStr, SQL_NTS);
cout << "Connecting ... " << endl;
if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
{
cout << "fail to connect" << endl;
}
else
{
cout << "connected" << endl;
}
return 0;
}
Upvotes: 0
Views: 10226
Reputation: 10646
Repaired your example and modified it to use a DSN-less connection
.
Tested under linux (fedora 32) with g++ code.cpp -std=c++17 -lodbc -o test.out
.
// https://stackoverflow.com/questions/39263791/odbc-connection-to-sql-server-database-c
// https://www.easysoft.com/developer/languages/c/odbc_tutorial.html
#include <sql.h>
#include <sqlext.h>
#include <iostream>
#include <string>
using namespace std;
void extract_error(
string fn,
SQLHANDLE handle,
SQLSMALLINT type)
{
SQLINTEGER i = 0;
SQLINTEGER native;
SQLCHAR state[ 7 ];
SQLCHAR text[256];
SQLSMALLINT len;
SQLRETURN ret;
cout << "\nThe driver reported the following diagnostics whilst running " << fn << "\n\n";
do
{
ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
sizeof(text), &len );
if (SQL_SUCCEEDED(ret))
printf("%s:%ld:%ld:%s\n", state, i, native, text);
}
while( ret == SQL_SUCCESS );
}
int main()
{
cout << "Program had started.." << endl;
SQLHENV env;
SQLHDBC dbc;
SQLRETURN ret;
SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
SQLRETURN SR;
cout << "Attempting Connection " << endl;
SQLCHAR sqlConnectionString [] = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1433;UID=SA;PWD=yourPassword;";
ret = SQLDriverConnect(dbc, NULL, sqlConnectionString, SQL_NTS,
outstr, sizeof(outstr), &outstrlen,
SQL_DRIVER_NOPROMPT);
cout << "Connecting ... " << endl;
extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
{
cout << "fail to connect" << endl;
}
else
{
cout << "connected" << endl;
}
return 0;
}
output
Program had started..
Attempting Connection
Connecting ...
The driver reported the following diagnostics whilst running SQLDriverConnect
01000:1:5701:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed database context to 'master'.
01000:2:5703:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed language setting to us_english.
connected
Upvotes: 3