ongoing ODBC project from C++ to SQL

Hi,

I'm new to programming and at my job I was givin the task of setting up an ODBC with c++ and SQL. I used some sample code and got it going however I'm not really sure how it works nor am I sure how to get it to connect to the SQL server.

I keep getting the error "Cannot find or open the PDB file"

If anyone can help me that'd be appreciated
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
#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){
    SQLCHAR sqlstate[1024];
    SQLCHAR message[1024];
    if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
        cout<<"Message: "<<message<<"\nSQLSTATE: "<<sqlstate<<endl;
}

int main(){
	SQLHANDLE sqlenvhandle;    
    SQLHANDLE sqlconnectionhandle;
    SQLHANDLE sqlstatementhandle;
    SQLRETURN retcode;

    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
        goto FINISHED;

    if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) 
        goto FINISHED;
    
    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
        goto FINISHED;

    SQLCHAR retconstring[1024];
    switch(SQLDriverConnect (sqlconnectionhandle, 
                NULL, 
                (SQLCHAR*)"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);
            goto FINISHED;
        default:
            break;
    }
    
    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
        goto FINISHED;

    if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"select * from testtable", SQL_NTS)){
        show_error(SQL_HANDLE_STMT, sqlstatementhandle);
        goto FINISHED;
    }
    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;
        }
    }

FINISHED:
    SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
    SQLDisconnect(sqlconnectionhandle);
    SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
    SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
    
}
And this is the debug output

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
'TEST.exe': Loaded 'C:\Users\Shahab Tran\Documents\Visual Studio 2010\Projects\TEST\Debug\TEST.exe', Symbols loaded.
'TEST.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\odbc32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\lpk.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\usp10.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'TEST.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'TEST.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\odbcint.dll', Binary was not built with debug information.
'TEST.exe': Loaded 'C:\Windows\System32\bcrypt.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\bcryptprimitives.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\sqlsrv32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\crypt32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\msasn1.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\netapi32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\netutils.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\srvcli.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\wkscli.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\version.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\sqlsrv32.rll', Binary was not built with debug information.
'TEST.exe': Loaded 'C:\Windows\System32\sspicli.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\odbccp32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\dbnetlib.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\ws2_32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\nsi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\security.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\secur32.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\cryptsp.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\credssp.dll', Cannot find or open the PDB file
'TEST.exe': Unloaded 'C:\Windows\System32\cryptsp.dll'
'TEST.exe': Loaded 'C:\Windows\System32\msv1_0.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\cryptdll.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\ntdsapi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\mswsock.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\WSHTCPIP.DLL', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\wship6.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\dnsapi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Program Files\Common Files\microsoft shared\Windows Live\WLIDNSP.DLL', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\psapi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\shlwapi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\IPHLPAPI.DLL', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\winnsi.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\rasadhlp.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\FWPUCLNT.DLL', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\schannel.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\ncrypt.dll', Cannot find or open the PDB file
'TEST.exe': Loaded 'C:\Windows\System32\netbios.dll', Cannot find or open the PDB file
Run-Time Check Failure #3 - The variable 'sqlstatementhandle' is being used without being initialized.
First-chance exception at 0x6d8b96ec in TEST.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x6d8b96ec in TEST.exe: 0xC0000005: Access violation reading location 0xcccccccc.
The thread 'Win32 Thread' (0x3608) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x346c) has exited with code -1073741510 (0xc000013a).
The thread 'Main Thread' (0x3088) has exited with code -1073741510 (0xc000013a).
The program '[10496] TEST.exe: Native' has exited with code -1073741510 (0xc000013a).
I have a whole tutorial on ODBC with compilable code here...

http://www.jose.it-berater.org/smfforum/index.php?board=378.0
oh thanks i would have never found that
Topic archived. No new replies allowed.