Link to home
Start Free TrialLog in
Avatar of turbot_yu
turbot_yuFlag for Singapore

asked on

how to use SQLDriverConnect

How to define the parameters.

      SQLHENV    hEnv;
      SQLHDBC    hDbc;
      SQLHWND     hwnd;
      

    SQLSMALLINT      SQL_NTS;      
    SQLCHAR *     szOutConn;
    SQLSMALLINT     MAX_CONN_OUT;
    SQLSMALLINT *     cbOutConn;
    //SQLUSMALLINT     SQL_DRIVER_NOPROMPT;

      SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"FILEDSN=SQL_Server;Trusted_Connection=yes""FILEDSN=SQL_Server;Trusted_Connection=yes"
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);

but has error
C:\Documents and Settings\yuming\Desktop\EventDTP 20 Feb 2006_ run online_June2006\eventDTP.cpp(676) : error C2064: term does not evaluate to a function
C:\Documents and Settings\yuming\Desktop\EventDTP 20 Feb 2006_ run online_June2006\eventDTP.cpp(680) : error C2660: 'SQLDriverConnect' : function does not take 7 parameters
Error executing cl.exe.
Avatar of deepu chandran
deepu chandran
Flag of Germany image

hi,

http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/cli/rzadpfndvcon.htm
The Following URL helps solve the Errors.
Please Go through it.It has an example code also

Deepu
Avatar of turbot_yu

ASKER

thank, now I use this, compile no error, is it correct?
      SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"FILEDSN=SQL_Server.dsn;Trusted_Connection=yes",
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);
it cannot connect when running, does it need to specify the path of dsn file?
      SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"FILEDSN=SQL_HMI_Server.dsn;Trusted_Connection=yes",
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);
Hi,

Check the Connection String You have given is currect or not .
it looks strange.
Deepu
I have a dsn file named SQL_HMI_Server.dsn under the path of C:\Program Files\Common Files\ODBC\Data Sources,
how to set the string?
Hi,

What connection you are using.If it is ODBC it just Look Like this,
ODBC;DSN=odbcName;UID=username;PWD=password;  >> For Example

Deepu
I am using File DSN for ODBC connection, because I set the default DB in the dsn File
HI,
OK you are using ODBC right. Then You should specify the odbc name in the Control string.
Have you create the DSN(From Control Panels >>Administrative Tools >> ODBC)

If u havent yet, then create it first and give that DSN name to the Contrl string

Deepu
From Control Panels >>Administrative Tools >> ODBC >> File DSN, I created a DSN file named SQL_Server.dsn, it saved in C:\Program Files\Common Files\ODBC\Data Sources
hi,

You just dont bother about the Path.
 SQLinfo = SQLDriverConnect(
          hDbc,
          hwnd,
(SQLTCHAR*)"ODBC;DSN=SQL_Server.dsn;UID=sa;PWD=;Trusted_Connection=yes;",
          SQL_NTS,
          szOutConn,
          MAX_CONN_OUT,
          &cbOutConn,
          SQL_DRIVER_NOPROMPT);
 Give username and  password(if present) to the Connection String

Deepu
still cannot,
In dsn file:
[ODBC]
DRIVER=SQL Native Client
UID=rmcs
PWD=rmcs
DATABASE=ming
WSID=HMI01
APP=Microsoft Data Access Components
SERVER=172.16.5.84\SQLEXPRESS
Does it need to initailize the parameter?

      SQLHENV    hEnv;
      SQLHDBC    hDbc;
      SQLHWND     hwnd;
    SQLSMALLINT      SQL_NTS;      
    SQLCHAR *     szOutConn;
    SQLSMALLINT     MAX_CONN_OUT;
    SQLSMALLINT      cbOutConn;
    MAX_CONN_OUT=1000;

      SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );
      SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER );
      SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc);
      SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"ODBC;DSN=SQL_Server.dsn;UID=rmcs;PWD=rmcs;Trusted_Connection=yes", //;Trusted_Connection=yes",
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);
hi,

You have create a File DSN ,fine.Then you should give the path also.I have posted the above comments for System DSN(Sorry for confused you)

"ODBC;FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\SQL_Server.dsn;UID=sa;PWD=;Trusted_Connection=yes;"

Deepu
still cannot, the return value is -1
Hi,

Try to use System DSN .I have already post the comment for that.You just check that if it works then we can make sure your code is fine.

Deepu
Bebug before conncet
-      &cbOutConn      0x0012d32c
            -13108
      hDbc      0x003c16f0
-      &hDbc      0x0012d33c "ð<"
            -16 'ð'
      hEnv      0x003c1628
      hwnd      0xcccccccc
      MAX_CONN_OUT      1000
      SQLinfo      -13108
-      szOutConn      0xcccccccc ""
            CXX0030: Error: expression cannot be evaluated
After connect
-      &cbOutConn      0x0012d32c
            -13108
      hDbc      0x003c16f0
      hwnd      0xcccccccc
      MAX_CONN_OUT      1000
      SQLinfo      -1
-      szOutConn      0xcccccccc ""
            CXX0030: Error: expression cannot be evaluated
Sysytem DSN cannot set default DB if more than 1 DB
SQLHENV    hEnv;
SQLHDBC    hDbc;
SQLHWND     hwnd;
SQLSMALLINT      SQL_NTS;      
SQLCHAR *     szOutConn;
SQLSMALLINT     MAX_CONN_OUT;
SQLSMALLINT      cbOutConn;
MAX_CONN_OUT=1000;

SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );
SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER );
SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc);
SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"ODBC;FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\SQL_Server.dsn;UID=rmcs;PWD=rmcs;Trusted_Connection=yes", //;Trusted_Connection=yes",
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);
Will they affect
SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );
SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER );
SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc);
Will it affect
-     szOutConn     0xcccccccc ""
          CXX0030: Error: expression cannot be evaluated
ASKER CERTIFIED SOLUTION
Avatar of deepu chandran
deepu chandran
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In file dsnj, it only give host name, then I can change host file in win to switch to dif server.
I tried DSN, but runtime error occur before the connection line

      SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc);       //Error LINE

      SQLinfo = SQLDriverConnect(
            hDbc,
            hwnd,
            (SQLTCHAR*)"ODBC;DSN=aaa;UID=rmcs;PWD=rmcs;Trusted_Connection=yes", //;Trusted_Connection=yes",
            SQL_NTS,
            szOutConn,
            MAX_CONN_OUT,
            &cbOutConn,
            SQL_DRIVER_NOPROMPT);

The Error is Unhandled Exception.
ok,error solved, but still cannot connect by DSN.
Hi,

You mean in FILEDSN?
Deepu

no, the popup error solved but still cannot connect
I use this one can conect,        
SQLinfo = SQLConnect( hDbc,
            (SQLCHAR*)"aaa",SQL_NTS,
            (SQLCHAR*)"rmcs",SQL_NTS,
            (SQLCHAR*)"rmcs",SQL_NTS );

but not sure whether it can switch DB
hi,
Make sure your authetication given to the ODBC  is Windows authetication
Deepu
I am not using win Au.