2

I am trying to run a console application written in C# using visual studio 2010 in which i am accessing a temp database of Microsoft Sql Server Managment Studio.

But the application gives exception as follows:

"Cannot open database "dbname" requested by the login. The login failed. Login failed for user `machinname\username""

And my connection string is as follows:

con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=temp;Integrated Security=True";

Why might this fail with such an error?

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
Nilesh
  • 135
  • 1
  • 2
  • 12

2 Answers2

6

The user that the application is running under (probably you since this is a console application), does not have login permissions on the database.

Integrated Security=True indicates that Windows Authentication is being used - either use a user that has appropriate permissions on the database, or grant the appropriate permissions to the user.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    How do i grant the permission to the user? – Nilesh Apr 05 '11 at 20:44
  • @user693730 - You need administrator rights on the database. Use Sql Server Management Studio - under the management area you can add users, logins and set the right access permissions. – Oded Apr 05 '11 at 20:47
  • As i am new to the Sql Server Management Studio i am not able to find management area – Nilesh Apr 05 '11 at 20:58
1

If you change the connection string to have a sql server username and password that can be associated with the database then it will work if the application is not running under a windows identity that is not associated with sql server windows authentication

Data Source=.\SQLEXPRESS;Initial Catalog=temp;Persist Security Info=True;User ID=<username>;Password=<pw>"

the application is running under a user that is not recognised in sql server

stack72
  • 8,198
  • 1
  • 31
  • 35