SQL Express Server 2008 connection

Hi good morning everybody,
I am currently trying to get to grips with C++ and am requiring to connect to an SQL Server the sample code i have found from google is:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <sqlext.h>

using namespace std;

SQLHANDLE sqlenvhandle = SQL_NULL_HANDLE; 
SQLHANDLE sqlconnectionhandle = SQL_NULL_HANDLE;
SQLHANDLE sqlstatementhandle = SQL_NULL_HANDLE;
SQLRETURN retcode;

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))
	{
		cout<<"Message: "<<message<<"\nSQLSTATE: "<<sqlstate<<endl;
		printf ("\n");
		printf("There has been an error.");
		getchar(); // waits for input 
	}
}

void CloseSQL ()
{
	SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
	SQLDisconnect(sqlconnectionhandle);
	SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
	SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
}


int main()
{

	if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
	{
		CloseSQL();
		goto END;
	}

	if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) 
	{
		CloseSQL();
		goto END;
	}

	if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
	{
		CloseSQL();
		goto END;
	}
	printf("Driver Initialised\n");

	SQLWCHAR retconstring[1024];
	switch(SQLDriverConnect (sqlconnectionhandle, 
		NULL, 
		(SQLWCHAR*)"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=MyDatabase;UID=sa;PWD=Admin-123;",
		SQL_NTS, 
		retconstring, 
		1024, 
		NULL,
		SQL_DRIVER_NOPROMPT))
	{
	case SQL_SUCCESS_WITH_INFO:
		show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
		break;
	case SQL_INVALID_HANDLE:
	case SQL_ERROR:
		show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
		CloseSQL();
		goto END;
	default:
		break;
	}
	printf("Connection made\n");

	if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
	{
		CloseSQL();
		goto END;
	}

	if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLWCHAR*)"select * from testtable", SQL_NTS))
	{
		show_error(SQL_HANDLE_STMT, sqlstatementhandle);
		CloseSQL();
		goto END;
	}
	else
	{
		char name[64];
		char address[64];
		int id;
		while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS)
		{
			SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &id, 0, NULL);
			SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, name, 64, NULL);
			SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, address, 64, NULL);
			cout<<id<<" "<<name<<" "<<address<<endl;
		}
	}

END:
	printf ("\n");
	printf("Program End, press enter key to exit!");
	getchar(); // waits for input 
	return 0;
}


My problem is that when I change the connection settings in:
 
(SQLWCHAR*)"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=MyDatabase;UID=sa;PWD=Admin-123;",

to my database name, user and password, It will not connect.
do i need to insert the Databases instance name somewhere?

Thank you
SQLWCHAR is likely a wide char. So you need to prepend L:

(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=MyDatabase;UID=sa;PWD=Admin-123;"

then you might not need the ugly cast.

but on the other hand:

SQLExecDirect(sqlstatementhandle, (SQLWCHAR*)"select * from testtable", SQL_NTS) works?
Topic archived. No new replies allowed.