2

I am attempting to connect to Microsoft SQL Server 2008 R2 from within Microsoft Visual Studio 2010. My connection string is as follows:

connection = New SqlConnection("Server=localhost;Database=greenapplication;User ID=WindowsID;Password=WindowsPassword")

I keep getting this exception: System.Data.SqlClient.SqlException:

Login failed for user 'WindowsID'.

What am I doing wrong? Are there certain permissions that I have to set up?

3
  • Does WindowsID refer to a windows domain account? If so, you are trying to use integrated security (incorrectly). Commented May 14, 2012 at 1:21
  • No, WindowsID refers to a local account on the same machine as Visual Studio and Microsoft SQL Server. Commented May 14, 2012 at 1:39
  • you can't use a local machine account for this (except the 'special' built-in ones that have domain equivalents), you would either need to use a domain account (or suitable other such as NETWORK SERVICE) with integrated security, OR a SQL Server account. Commented May 14, 2012 at 1:52

3 Answers 3

1

If you are trying to use integrated security, your connection string should be:

"Server=localhost;Database=greenapplication;Trusted_Connection=True"

You then manage permissions through roles and windows groups. (You can grant individual users permissions, but this is not the recommended way to manage database access).

UPDATE: You neglected to mention in original question that this is a web application. The usual way to handle this is to create an app. pool running under a known identity (say NETWORK SERVICE), and then give that identity the necessary permissions on the database.

Here's an example:

-- Create a SQL Server login for the Network Service account
sp_grantlogin 'NT AUTHORITY\Network Service'

-- Grant the login access to database
USE MyDB
GO
sp_grantdbaccess 'NT AUTHORITY\Network Service', 'Network Service'

-- Add user to read only database role
USE MyDB
GO
sp_addrolemember 'db_datareader', 'Network Service'

If you insist on using the less secure method of passing of username and password in connection string, use SQL Server logon:

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

2 Comments

I now get this error: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\DefaultAppPool'.
Sorry for the neglect, but this still does nothing.
0

since you said "WindowsPassword", I think you're probably supposed to be using this connection string:

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

I'd also advise visiting this site:

http://www.connectionstrings.com

2 Comments

I now get this error: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\DefaultAppPool'
You'll need to make sure this user is configured in SQL Enterprise Manager and that it has permission to login to the database. While you're at it, you'll also need to grant it access to the tables you intend to use.
0

You can try this.

"Provider=SQLOLEDB;Data Source=" & SQLSERVER NAME & "; Initial Catalog=" & Dbname & ";Network=DBMSSOCN;" & _
            "User Id=" & UID & ";" & _
            "Password=" & Pwd & ";"

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.