3

I keep receiving a connection error. I know that the information is right, as I tried them out with SSMS now several times in the last few minutes, so the problem is in the C# connection string. According to various Google searches, the syntax is right, just it does not work.

try
{
    // Get the connection information.
    GetSQLConnectInfo(ref sConnect);

    // Formulate the connection string.
    String strConnect = String.Format("Server=myserver;Database=mydb;User Id=myusername;Password=mypassword;Trusted_Connection=yes;connection timeout=30");

    // DATABASE: Create the connection.
    SqlConnection oConnection = new SqlConnection(strConnect);
    oConnection.Open();
    if (ConnectionState.Open != oConnection.State)
        return false;

    return true;
}

catch
{
}

Error comes back with: Login failed for user 'myserver\Guest'. Error Code: -2146232060 Error Number: 18456

It would seem that judging by the error message, the Open method does not recognize the user name, as Open tries to use a guest account, which obviusly will not work.

Am I missing something? I am using SQL Server authentication.

3
  • what platform you are using? Commented Jan 3, 2013 at 19:06
  • 2
    trusted_connection=yes doesn't match to using sql authentication. Commented Jan 3, 2013 at 19:07
  • why it's User Id instead of UserId or User_Id ?? Commented Jan 3, 2013 at 19:07

1 Answer 1

8

Remove Trusted_Connection=yes. That will override your SQL Authentication (username/password) settings and try to log in as the user running the app.

From the documentation:

If the value of the Trusted_Connection key is "Yes", both the UID and PWD keys are ignored. Otherwise, the UID key has to be specified.

Sign up to request clarification or add additional context in comments.

4 Comments

That was so not obvious. I simply borrowed a one of the first connection strings that I saw. I had no idea that Trusted_Connection overrides the user name and password. Thank you.
connectionstrings.com - Great resource if you find yourself needing to look up connection strings again.
I came here to realize that IIS was not allowing my application pool to go through to my DB using Trusted_Connection=yes. Had to put in the credentials manually in my Web.config
You need to get Kerberos working to enable trusted connections with more than one hop (client-->IIS-->DB), which is not simple. It's more secure, but harder to get right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.