Connecting to Microsoft SQL Server from Visual Basic

14,965

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:

Share:
14,965
Kruug
Author by

Kruug

I specialize in help desk/desktop support. I dabble in both Windows and Linux, as well as a bit of programming (mainly web-based currently).

Updated on June 04, 2022

Comments

  • Kruug
    Kruug almost 2 years

    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?