[unixODBC-support] unixODBC2.3.1 connection to ms sql server 2008 R2

Duncan Kerr Duncan.Kerr at dealhub.com
Wed Jan 4 12:19:19 GMT 2012


Im having problems connecting from C++, even though I can connect via my DSN with isql.  MS sqlcmd works fine, and the driver/unixODBC seem to be installed correctly.  My code fails wioth SQL_INVALID_HANDLE returned by  SQLDriverConnect because the internal list of connections is empty (__validate_dbc( connection ) fails) . Here are details of my system and code.  Any suggestions would be greatfully accepted.

#### can we see the database - yes ######
duncank at DHFS0030:~/workspace/odbc1/Debug% sqlcmd -S dhws0049,1599 -U sa -P ******
1>
Sqlcmd: Warning: The last operation was terminated because the user pressed CTRL+C.

##### odbc isql ok? ############
isql SQLSERVER_49 sa ******
+-------------------------------------------+
|  Connected!
|
.
.
SQL>

####### shared lib ok? ############
duncank at DHFS0030:~% ll /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
-rwxr-xr-x. 1 root root 1720536 Jan  3 12:12 /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0

duncank at DHFS0030:~/workspace/odbc1/Debug% ldd /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
linux-vdso.so.1 =>  (0x00007fff0504d000)
libcrypto.so.6 => /usr/lib64/libcrypto.so.6 (0x00007fa6f1986000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fa6f1781000)
libodbc.so.1 => /usr/lib64/libodbc.so.1 (0x00007fa6f1515000)
librt.so.1 => /lib64/librt.so.1 (0x00007fa6f130d000)
libssl.so.6 => /usr/lib64/libssl.so.6 (0x00007fa6f10b1000)
libuuid.so.1 => /lib64/libuuid.so.1 (0x00007fa6f0ead000)
libodbcinst.so.1 => /usr/lib64/libodbcinst.so.1 (0x00007fa6f0c96000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fa6f09bb000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fa6f0782000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fa6f047b000)
libm.so.6 => /lib64/libm.so.6 (0x00007fa6f01f6000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fa6effe0000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa6efdc3000)
libc.so.6 => /lib64/libc.so.6 (0x00007fa6efa43000)
libz.so.1 => /lib64/libz.so.1 (0x00007fa6ef82e000)
/lib64/ld-linux-x86-64.so.2 (0x0000003a47800000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fa6ef62b000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fa6ef401000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fa6ef1f7000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fa6eeff5000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fa6eedda000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fa6eebbb000)

####### account odbc ini file ###########
duncank at DHFS0030:~% cat ~/.odbc.ini
[SQLSERVER_08]
Driver                   =/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
Server                   = DHFS0008,4267

[SQLSERVER_49]
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
Server=DHWS0049,1599
Database=DEAL_HUB_FLYNN

########### cat /etc/odbcinst.ini ######################
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbc.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbc.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1

[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1

[ODBC]
TraceFile=/tmp/sql.log
Trace=Yes

[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
UsageCount=2

###my code - was pretty much as you specified ########
    SQLRETURN retcode;
    SQLHENV                         henv;
    SQLHDBC                         hdbc;


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

    if(SQL_SUCCESS!=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0))
                                goto FINISHED;

    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc))
                goto FINISHED;

    SQLCHAR retconstring[1024];
    SQLSMALLINT dsnlen;
    retcode = SQLDriverConnect (hdbc,
                NULL,
                //forum
               (SQLCHAR*)"dsn=SQLSERVER_49;uid=sa;pwd=******",
                SQL_NTS,
                retconstring,
                1022,
                &dsnlen,
                SQL_DRIVER_NOPROMPT);

                                switch(retcode){
                                case SQL_SUCCESS:
                                cout << "ok" << endl;
                                break;
                                case SQL_INVALID_HANDLE:
                                cout << "sqldriverconnect invalid handle" << endl;
                                break;
                                default:
                                cout << "sqldriverconnect other error" << endl;
                                }

Duncan Kerr



________________________________________________________________________
This message may contain privileged and confidential information 
intended only for the use of the addressee named above. If you are not 
the intended recipient of this message you are hereby notified that any
use, dissemination, distribution or reproduction of this message is 
prohibited. If you have received this message in error please notify
the sender immediately. Any views expressed in this message are those of
the individual sender and may not necessarily reflect the views of 
the company for whom they represent.
________________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-support/attachments/20120104/2abdf634/attachment.html>


More information about the unixODBC-support mailing list