Share via


SQL Connection string ignoring UserID

Question

Tuesday, January 23, 2018 12:31 PM

Using the code below to connect to a SQL server and for half the users it connects using the user "john" from the query string and connects fine but others its using the same query string but ignores the userID and uses their windows id instead.

Has anyone else come across this and why it ignores the userID?

Thanks,

John

using (SqlConnection connection = new SqlConnection("Data Source=sqlServer;Database=dbName;User Id=john;"))
{
       connection.Open();
        // Do work here; connection closed on following line.
       connection.Close();
}

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx

All replies (6)

Tuesday, January 23, 2018 12:38 PM

maybe it's where you should post.

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver


Tuesday, January 23, 2018 12:46 PM

Hi John,

Try adding integrated security to false i your connection string like below:

using (SqlConnection connection = new SqlConnection("Data Source=sqlServer;Database=dbName;User Id=john;Integrated Security = false;"))

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, January 23, 2018 1:49 PM

Connection string looks good, have you tried setting the default catalog? Usually user name and pass words are setup at the catalog, not the server level

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;

https://www.connectionstrings.com/sql-server/

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Wednesday, January 24, 2018 9:30 AM

Hello JohnA-83,

The issues is caused by Sql Server authentication mode. It could be windows authentication or sql server authentication.

If you want to connect sql server via windows authentication, the connection string like below.

Data Source=.\SQLEXPRESS;Database=T1;Integrated Security=SSPI;"

OR

Data Source=.\SQLEXPRESS;Database=T1;Integrated Security=true;"

In this way the sql server don't care if you write the user id and password. Even if user and password is wrong the connection could connect successfully.

If you want to connect sql server via sql server authentication, the connection string like below.

Data Source=.\SQLEXPRESS;Database=T1;User Id=xx;Integrated Security=false;

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Sunday, January 28, 2018 1:26 PM

Hello JohnA,

Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

Thank you for your understanding and cooperation.

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Friday, February 2, 2018 11:26 AM

Narrowed it down to the Office VSTO addin which we use to run a standard console application, this application works fine if you run it outside of the addin but get these issues when running from inside for some users! Going to have to go to the office addin forms.

Thanks for you help guys are any of you good with VSTO addins?