ODBC cannot connect

Good morning guys,

I've been working on a data-mining project using libcurl and have encountered an issue with my odbc database connection.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <sqlext.h>
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))
		wcout << "Message: " << message << "\nSQLSTATE: " << sqlstate << endl;
}

int main() {

	SQLHENV env;
	SQLHDBC dbc;
	SQLHSTMT stmt;
	SQLRETURN ret;
	SQLSMALLINT columns;
	int row = 0;

	/* Allocate an environment handle */
	SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
	/* We want ODBC 3 support */
	SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
	/* Allocate a connection handle */
	SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

	/* Connect to the DSN */
	SQLDriverConnectW(dbc, NULL, L"DRIVER={SQL Server};ERA-PC-STUART\\JBK_DB;DATABASE=master;UID=geo;PWD=kalle123;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);

	/* Check for success */
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt))
	{
		show_error(SQL_HANDLE_DBC, dbc);
		std::cout << "Failed to connect";
	}
	if (SQL_SUCCESS != SQLExecDirectW(stmt, L"select salary from dbo.salary_table", SQL_NTS)) {
		show_error(SQL_HANDLE_STMT, stmt);
	}
	else {
		int id;
		cout << "ID:" << endl;
		while (SQLFetch(stmt) == SQL_SUCCESS) {
			SQLGetData(stmt, 1, SQL_C_ULONG, &id, 0, NULL);
			cout << id << endl;
		}
	}

	return 0;
}


It gives me this output:

Message: [Microsoft][ODBC Driver Manager] Connection Not Open
SQLSTATE: 08003
Failed to connect


I'm running a Visual studio c++ application and I'm attempting to connect to a SQL Server 2014.

In addition to above information I successfully ran this application on my laptop with no errors and I've set up a SQL Server Native Client 11.0 ODBC (just as I have on my laptop) that connects successfully.

Any advice would be greatly appreciated.

Best regards,
Joakim
Last edited on
Does it not follow then that, since the same code works in various contexts, i.e., your laptop, and some other device, but does not work in some other context, that there might be something wrong with the connection string for the context in which the code fails?

added later....

Good chance SQLGetDiagRec() with a connection handle will tell you what's wrong.
Last edited on
Topic archived. No new replies allowed.