Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to connect to SQL Server database using ODBC API with DSN only?

0 views
Skip to first unread message

Dmitry Khodorkovsky

unread,
Sep 14, 2001, 7:25:07 PM9/14/01
to
Hi,

does anyone know how to connect to SQL Server database if you have DSN
stored in System DSN with ODBC API.
and it should not be w/o user additional input.
Seems like SQLConnect, SQLDriverConnect don't work.
SQLDriverConnect is ok there only when you are using Windows
Authentication. I need SQL Server Authentication.

Any ideas?

Dmitry

William DePalo [MVP]

unread,
Sep 15, 2001, 1:06:17 AM9/15/01
to
"Dmitry Khodorkovsky" <dkh...@home.com> wrote in message
news:eAiVpRXPBHA.1496@tkmsftngp03...

Does your connection string include the id and password of the user:

DSN=blah;UID=user;PWD=password

In an old project of mine I have this line

rc = SQLDriverConnect(hDbc, hWnd, DSN, SQL_NTS,
szConn, sizeof(szConn), &cbConnOut,
SQL_DRIVER_COMPLETE);

where hDbc is a handle to a connection, hWnd is a handle to a window that
will parent any dialog boxes and where szConn is formatted as above. I don't
have a SQL Server system handy to test with now, but I seem to recall that
version 6 would connect without displaying any addition dialog boxes with
the user id and password in the connection string.

If you don't plan to ask the user for credentials you have the sticky
problem of how to store then securely and how to handle the case when they
are changed.

Regards,
Will


Dmitry

unread,
Sep 15, 2001, 1:08:35 PM9/15/01
to
Hi,

No, I don't put UID and PWD in Connection String -- I can't --- the reason
is that code must be flexible and these parameters were provided when DSN
was created.
Second --- in HWND we have NULL as it must not have any interactions with
the user.
--- App is the NT Service, so on its start time it must connect w/o waitinig
for admin to come and punch login and password


"William DePalo [MVP]" <depalow...@compuserve.com> wrote in message
news:e24sPOaPBHA.1784@tkmsftngp05...

William DePalo [MVP]

unread,
Sep 15, 2001, 2:24:22 PM9/15/01
to
"Dmitry" <dkh...@home.com> wrote in message
news:#EPOJjgPBHA.404@tkmsftngp03...

> No, I don't put UID and PWD in Connection String -- I can't --- the reason
> is that code must be flexible and these parameters were provided when DSN
> was created.
> Second --- in HWND we have NULL as it must not have any interactions with
> the user.
> --- App is the NT Service, so on its start time it must connect w/o
waitinig
> for admin to come and punch login and password

As I said, I don't have a SQL Server system to investigate.

When I needed to connect automatically, I inspected the parameters that the
driver returned from the call to SQLDriverConnect(). Then I saved those
parameters and used them on subsequent connect operations. If the driver
gets all of the parameters it needs, it shouldn't have to display any dialog
boxes.

But I did this long enough ago in a application meant to support many DBMS
systems that I am hazy as to whether I was able to connect automatically to
SQL Server or not.

Regards,
Will


yugami

unread,
Sep 17, 2001, 2:10:52 PM9/17/01
to
if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,
&henv))){
if (SQL_SUCCESS == (rc = ::SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER))){
if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc))){
rc = ::SQLConnect(hdbc, pszSourceName,
SQL_NTS, pszUsername, SQL_NTS, pszPassword, SQL_NTS);

sourcename is the name of the system source, username and password are setup
earlier through whatever means you want.

"Dmitry Khodorkovsky" <dkh...@home.com> wrote in message
news:eAiVpRXPBHA.1496@tkmsftngp03...

0 new messages