Link to home
Start Free TrialLog in
Avatar of amehanov
amehanovFlag for Afghanistan

asked on

,Net Console App cannot connect to SQL server w/ correct credentials

I am writing a .Net console application.  Using MS SQL 2005 and .Net 4.  To connect to SQL I am using the SqlConnection class.  The code works just fine on my local machine against my local db.  However, when I change the connection string to point to a network SQL server, it throws an exception indicating that the login failed.  I know the credentials work against the db as they are the same credentials that many of our web apps are using and they have no problem.  I get the same result (against the remote SQL server) running the code either locally or on another server.  Can anyone shed some light on this?  
Avatar of kaufmed
kaufmed
Flag of United States of America image

Can you post a sanitized copy of your connection string?
Avatar of amehanov

ASKER

szConn = @"Server=xxxxx;Initial Catalog=Users;uid=domain\user;pwd=password";
Hmmm...  Was this paraphrased? I've understood passing a username and password in this manner (note User Id and Password):

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Open in new window

It cannot be uid=domain\user;pwd=password, that implies you are mixing Windows Authentication with SQL Server Authentication.

It is either standard:
Server=xxxxx;Database=Users;User ID=myUsername;Password=password;Trusted_Connection=False;

Or Trusted:
Server=xxxxx;Database=Users;Trusted_Connection=True;
Disregard http:#35268023 . In browsing the MSDN, I see you can use either; www.connectionstrings.com didn't show it when I looked at it.
So, just to be sure, I changed my connection string to:

szConn = @"Server=server;Database=Users;User ID=domain\user;Password=pwd;Trusted_Connection=False;";


Exact same result.  
>>Exact same result.   <<
As I stated you cannot do it that way.  You need to decide whether you want to use Windows Authentication or SQL Server Authentication.

So how about we ask this way:
Is the login you are trying to use was it setup using Windows Authentication or SQL Server Authentication?

I read your post and II changed the connection string to the standard one (as per your specification in your post) when I last posted.   And it does not work.  I get the same result as the original connection string.  
Unfortunately, if you are using this:
szConn = @"Server=server;Database=Users;User ID=domain\user;Password=pwd;Trusted_Connection=False;";

Than this is not a login using SQL Server Authentication.
That being the case, then if I understand you, I have to use the following connection string:

Server=xxxxx;Database=Users;Trusted_Connection=True;

And if that is the case, the app will run under the credentials of who ever is logged in - and that means whoever wants to run this .exe must have their user account added to the db?  
>>and that means whoever wants to run this .exe must have their user account added to the db? <<
That is correct.  Regardless of how you connect to a database (SQL Server Authentication or Windows Authentication) that login is required to be added in the database in order for it to be accepted.
Is there any way to configure the connection string to allow the app to log in as a particular domain user (so that any individual can use the app and not need their account added to the db)?  
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America 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