There is a database, in the C # console application I try to fetch the database and read the contents of the table as follows:

string cs= @"Data Source=.\SQLEXPRESS;Initial Catalog=ShopDB;Integrated Security=True;"; string command = "SELECT * FROM Customers"; DataTable customers = new DataTable(); using (SqlConnection connection = new SqlConnection(cs)) { connection.Open(); SqlCommand cmd = new SqlCommand(command, connection); SqlDataReader dr = cmd.ExecuteReader(); customers.Load(dr); dr.Close(); } 

When I try to open a connection, I get an error

 System.Data.SqlClient.SqlException: 'Cannot open database "ShopDB" requested by the login. The login failed. Login failed for user 'MicrosoftAccount\mail@yahoo.com'.' 

I use MS SQL Server Express 2016 DB, Windows 10, VS 2017. In the DB, Authentication method is selected by Windows Authentication. When you try to specify in the connection string the user name, which is displayed when connecting to the database (in the inactive fields) and the password that corresponds to this user, an error appears

 System.Data.SqlClient.SqlException: 'Login failed for user 'DESCTOP-5HGPGMV\username'.' 

Tell me, please, what is the error.

Closed due to the fact that off-topic by PashaPash member Jul 23 '17 at 9:45 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Integrated Security=True appears in your connection string. This means that the login of the user from which the program is running will be used to connect to the SQL server. For example, if the program is started by the user mydomain\Ivanov , then the login mydomain\Ivanov will be used to connect to the MS SQL server, and if the program starts mydomain\Petrov , then the login mydomain\Ivanov .

    When trying to specify in the connection string the user name that is displayed when connecting to the database (in the inactive fields) and the password that corresponds to this user

    Login and password in the connection string for this mode is not necessary!

    Just run sql server management studio and in Security - Logins make sure that the user DESCTOP-5HGPGMV\username is in the list, plus it has sufficient rights to the database of your application.

    SSMS, security - logins

    Alternatively, you can remove the Integrated security and then you will need to specify both the username and password from any SQL Server account.

    For a general understanding, you can read these questions on so:

    • Thanks for the answer and especially for the link, I solved the problem by changing the connection string. In addition, I realized that I had 3 server instances: SQLEXPRESS, SQLEXPRESS01, MSSQLSERVER and I tried to connect not to where I created the database with the table. - Ilia Mikhailov