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
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:
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=SSPIto your connection string.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.