简体   繁体   中英

Cannot connect to Access Database using C++

My subject is somewhat stupid and complex for me. I'm trying to make a very simple connection with an Access 2007 db, but the connection never happens. I'm trying to see what happens with SQLGetDiagRec(), but the program crashes when SQLGetDiagRec() is executed. I'm not that much into C++, so I'm stuck for the past few days in this. Any help will be highly appreciated. I'm using Visual C++ 2008.

EDIT: After changing the character set from Unicode to Multibyte, I was able to execute the SQLGetDiagRec. I, then, changed my two pointers from int and char to SQLSMALLINT and SQLCHAR and, bam, it worked. Thanx a lot for the heads up guys.

#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <iostream>
#include <sql.h>
#include <sqltypes.h>
#include <sqlext.h>
#include <stdio.h>
#include <stdlib.h>


int main(){
    char szDSN[256] = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DSN='Hospital_mdb';DBQ=C:\\Hospital.mdb;";
    SQLHANDLE EnvHandlePtr;
    SQLHANDLE ConHandle;
    SQLHANDLE StmtHandle;
    SQLRETURN rc;

    SQLSMALLINT iConnStrLength2Ptr; // Changing from int     iConnStrLength2Ptr;
    SQLCHAR szConnStrOut[256]; //changing from char    szConnStrOut[256];
    SQLCHAR SQLState[6], Msg[SQL_MAX_MESSAGE_LENGTH];
    SQLINTEGER NativeError;
    SQLSMALLINT MsgLen;


    if ( (rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvHandlePtr)) == SQL_SUCCESS){
        printf("Environment Set!");
        if( (rc = SQLSetEnvAttr(EnvHandlePtr, SQL_ATTR_ODBC_VERSION,
            (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER))==SQL_SUCCESS ){
                printf("Driver Set!");
                if ( (rc = SQLAllocHandle(SQL_HANDLE_DBC, EnvHandlePtr, &ConHandle))==SQL_SUCCESS ){
                    printf("Allocation Done!");//**so far, so good, but then the connection doesn't happen**
                    rc = SQLDriverConnect(ConHandle, NULL, (SQLWCHAR*)szDSN, 
                        SQL_NTS, (SQLWCHAR*) szConnStrOut, 0, (SQLSMALLINT*) iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
                    if ( rc == SQL_SUCCESS ){
                    //if ( (rc = SQLConnect(ConHandle, (SQLWCHAR*)szDSN, SQL_NTS, (SQLWCHAR*)"", SQL_NTS, (SQLWCHAR*)"", SQL_NTS))== SQL_SUCCESS ){
                        printf("Connection Done");
                    }//end of Connection clause
                    else{   
                        SQLGetDiagRec(SQL_HANDLE_DBC, ConHandle, 1, (SQLWCHAR*)SQLState, &NativeError, (SQLWCHAR*)Msg, sizeof(Msg), &MsgLen);
                            printf("Connection Failed\n%s", Msg);
                    }
                    SQLDisconnect(ConHandle);
                }//end of Connection Allocation clause
                SQLFreeHandle(SQL_HANDLE_DBC, ConHandle);
        }//end of Driver clause
        SQLFreeHandle(SQL_HANDLE_ENV, EnvHandlePtr);
    }//end of Enviroment clause

    _getch();

}//end of main

You are casting the diagnostics that the compiler generates for your bad code away. At least SQLState is bad, you are passing a 6 byte buffer when 12 bytes are written. That corrupts the stack frame. The SQLDriverConnect call cannot work for the same reason.

Remove all casts by declaring your local variables properly.

And use Unicode in your code. You could disable it in your project settings but that's never not a mistake when you work with a dbase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM