0

I am trying to establish a connection to a Microsoft Sql Server express edition databse called PrimeMinisters and get the following error.

Cannot open database "PrimeMinisters" requested by the login. The login failed. Login failed for user 'DESKTOP\Arian'. 

I am asuming I need a user name and password but as this is just for home use I dont see the need for a username and password in the connection string.

I added the database as a new connection in database explorer and this is the connection string I added to the eventhandler.(desktop is the name of my pc)

 try
    {
        string connect = @"Data Source=DESKTOP\SQLEXPRESS; Initial Catalog=PrimeMinisters; Trusted_Connection=Yes;"; 



        SqlConnection sqlConn = new SqlConnection(connect);
        sqlConn.Open();

        LabMessage.Text = "A connection has been established";
    }
    catch (Exception ex)
    {
        LabMessage.Text = ex.Message.ToString();
    }

How can I overcome this error message and establish a connection with the database?

Kind regards

2
  • 2
    May be u need windows auth? Try to use "Integrated Security=True" in your connection string Commented Nov 28, 2012 at 13:26
  • Can you connect to SQL Server using SQL Server Management tools? How to you authenticate there? Do you enter username and password or use your Windows identity? Commented Nov 28, 2012 at 13:27

1 Answer 1

3

Your application is correctly establishing communication to SQL Server but the server does not authorize your current user (DESKTOP\Arian) to connect. You need to enter authentication information for account which is allowed to use SQL Server.

SQL Server allows two modes of authentication:

  1. Windows authentication

where authorization is performed against Windows account under which connecting application is running. In your case, your application is running under DESKTOP\Arian. To use this you should add Integrated Security=SSPI to your connection string.

  1. SQL Server authentication

where accounts are created just for the SQL Server instance. This can be done during setup or later by issuing appropriate commands with user that has sufficient privileges. To use this type of authentication set integrated security to false (Integrated Security=false) and set valid user name and password (User Id=Arian;Password=secretsquirrel)

If you have forgotten all passwords you can take a look at this answer for instructions on how to reset a password.

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

Comments

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.