Solved

ODBC Connect to DSN with Username and Password

Posted on 2007-11-19
12
7,844 Views
Last Modified: 2008-10-07
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.
0
Comment
Question by:cuziyq
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 84 total points
ID: 20313868
If the user name and password are already configured in the datasource, just pass NULL for these parameters.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 83 total points
ID: 20313945
AFAIK, there is no way to use SQLConnect with no valid user authentification. But you can try passing "DEFAULT" both to user and password.

If that doesn't work (the docs say that user and password were passed to the ODBC driver, so the behavior is driver specific), you can try to make a SQLDriverConnect passing "DSN=datasource" as connect string. If it prompts for user and password you may try "DSN=datasource;UID=DEFAULT;PWD=DEFAULT". If that doesn't work either, the only chance I know is to use 'Windows authorization' in the SQL database, i. e. each user can login with his/her Windows user and password. If so, the connect string would be "DSN=datasource;Trusted_Connection=yes".

Regards, Alex

0
 
LVL 14

Author Comment

by:cuziyq
ID: 20314205
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20314397
>>>> 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
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20314443
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.

 
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 83 total points
ID: 20315748
Hi There,

Here is the sample.It requires a data source name and accepts an optional user ID and password. It works well for applications that hard-code a data source name and do not require a user ID or password

http://msdn2.microsoft.com/en-us/library//ms714163.aspx

Hope this helps.

Best Regards,
DeepuAbrahamK
0
 
LVL 14

Author Comment

by:cuziyq
ID: 20412066
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20412883
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  
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20442468
>>>> 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.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20479019
Split amongst participating Experts.

modus_operandi
EE Moderator
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 36
Powershell SMO script not working. 18 101
Updating variable table 9 17
SQL Date Retrival 7 30
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now