Link to home
Start Free TrialLog in
Avatar of cuziyq
cuziyq

asked on

ODBC Connect to DSN with Username and Password

Connecting to a SQL database using ODBC in C++.  The SQLConnect(), function takes DSN name, username, and password strings as its params.  I have created a system DSN with a username and password already configured.  How do I tell SQLConnect that I wish to use the default authentication information in the DSN?  I've tried calling SQLConnect with nulls for the username and password, but that doesn't work.  Specifying the user name and password manually does work, but I do not want to hard code it or have to prompt the user for it.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
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
SOLUTION
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
Avatar of cuziyq
cuziyq

ASKER

jkr, I tried passing in NULLs for the user name and password, and all I got was a driver manager error:
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user (null). Reason: Not associated with a trusted SQL Server connection.

Alex, it's the same story with "DEFAULT".

I find it very hard to believe that the ODBC data source administrator allows you to create a DSN for SQL Server that contains fields for user name and password (among many other things), test it, and yet not provide a mechannism by which you can actually use that user name and password.  Microsoft's documentation doesn't mention it one iota as far as I can see.  Very strange indeed.
>>>> that the ODBC data source administrator allows you to create a
>>>> DSN for SQL Server that contains fields for user name and password
>>>> (among many other things), test it, and yet not provide a mechannism
>>>> by which you can actually use that user name and password.
The ODBC interface was first. The DSN information was in ODBC.INI and ODBCINST.INI. Data source administrator comes much later and was used for other purposes than supporting 'native ODBC' interface, e. g. DAO, ADO (OLE DB), JDBC, EXCEL. The native ODBC drivers would have to handle default behavior but there is no way to get credentials (user, password) as plain text from registry. Instead, programs or drivers which can handle the default credentials in the registry have an additional interface to connect to database where they could/would pass the internal credential structures. That is MS specific and hardly can be part of an open standard like ODBC.

Regards, Alex
I used to solve the problem by either using 'trusted_connection'  (what is a must for ODBC on DB2), or by using a login prompt, or by encrypting user and password with a valid encryption method and store them in a config file. That maybe uncomfortable but if you could read users or passwords from registry would be worse.

 
SOLUTION
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
Avatar of cuziyq

ASKER

None of the suggestions work.  I have a suspicion that the only reason for having a username and password in the DSN configuration is so that the Test... button can log on to it.  I have written the software to have a config section that is user-configurable.
I object against deleting the question.

The answers didn't find a solution for that the asker expected and wanted, but none of the comments was wrong. The thread may be worthful if there is a similar question in the future. And, the author found a workaround which also was based on suggestions made here in that thread.

Regards, Alex  
>>>> Could you recommend one or more comments as the answer?
Hmmm. Valuations are a bad job (and I deeply admire you when you have to do so). As said, none of the answers given is really wrong, so you may equally distribute between the experts or the answers. I could tell more but as I participated with the most comments, I hardly could be regarded to be objective.
Split amongst participating Experts.

modus_operandi
EE Moderator