Sql connection C# login failed

0

i cannot connect via C# to my database and it's making me crazy.

I know my credentials are good because I can open my SQLServerMangement with them and query the database.

I always get login failed!!

I'm trying with a buch of different connection string and nothing works.

--> System.Data.SqlClient.SqlException (0x80131904): Login failed for user

        var connectionStrings = new List<string>();

        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=myuser;Password=mypassword");
        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=mydomain/myuser;Password=mypassword");
        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=mydomain\\myuser;Password=mypassword");
        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=mydomain\\\\myuser;Password=mypassword");
        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=mydomain//myuser;Password=mypassword");
        connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=mydomain\\myuser;Password=mypassword");


        foreach (var con in connectionStrings)
        {
            SqlConnection cnn ;
            cnn = new SqlConnection(con);
            try
            {
                cnn.Open();
                Console.WriteLine("Connection Open !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
                cnn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can not open connection ! ");
                //Console.WriteLine(ex.ToString());
            }

        }
c#
sql-server
sqlconnection
asked on Stack Overflow Sep 30, 2020 by Shan9588 • edited Oct 1, 2020 by jarlh

2 Answers

4

The mydomain\myuser attempts indicate you might be using Integrated Security or Trusted_Connection. In that case, the connection string looks like this:

Server=myserver;Database=mycatalog;Trusted_Connection=True;

or this

Server=myserver;Initial Catalog=mycatalog;Integrated Security=True;

There is no combination here that allows you to specify a username or password. If you want to use a username/password, you must setup sql authentication and create a separate sql account. There is no way you can manually specify Active Directory or Windows login credentials to connect to a database, other than running the program as the user that is making the connection.

answered on Stack Overflow Sep 30, 2020 by Joel Coehoorn
1

The right way it's:

connectionStrings.Add("Data Source=myserver;Initial Catalog=mycatalog;User ID=myuser;Password=mypassword");

Also, you can check https://www.connectionstrings.com/ https://www.connectionstrings.com/sql-server/

Finally, check if the server have published the port 1433, maybe you try to connect remotely the server and the port is blocked by Firewall or the Port is Disable for remote connections. (And you only can connect locally using Pipe or Message Exchange). Check "SQL Server Configuration Manager"

answered on Stack Overflow Sep 30, 2020 by Mauricio Kenny

User contributions licensed under CC BY-SA 3.0