StatsViaCsh
StatsViaCsh

Reputation: 2640

SQL Server Express 2008 connection issue; trying to connect to localhost

I've become stuck with an issue that I am sure is on- the- nose simple to fix, but this is my first time trying to connect to SQL Server Express 2008 R2. It's on my local machine, and my error is the standard:

"Cannot open database "Market" requested by the login. The login failed. Login failed for user 'KR\user'."

In SSMS, here are some of the connection properties: Authentication Method: Windows Authentication User Name: KR\user Server Name: KR\SQLEXPRESS Instance Name: SQLEXPRESS

Here is my connection string: Data Source=localhost\SqlExpress;Initial Catalog=Market;Integrated Security=True

Here is how I'm attempting to connect via C#:

connectionString = "Data Source=localhost\\SqlExpress;Initial Catalog=Market;Integrated Security=True";    
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();

Edit:My attempt with a different syntax was this connection string:

"Data Source=.\SQLExpress;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Market.mdf';Integrated Security=True;

Using it with the same calling code changes the error to:

An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Market.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Edit: I found the properly formatted connection string by right- clicking the database in database explorer, going to properties, then copy/ pasting the connection string property into my code. Thx...

Upvotes: 0

Views: 2019

Answers (2)

Oded
Oded

Reputation: 498904

You need to setup the permissions for this user on your instance of SQL Server Express.

A server login (using Windows Authentication), a database user that is attached to the login and the permissions on the databases this user will be accessing all need to be setup.

Upvotes: 1

Brian Minnich
Brian Minnich

Reputation: 402

Looks like you NT security. Have checked that the "KR\user" has been granted rights on the Marketing database. Open the database "Market" > Security > Add User. Grant them DBO rights and try again.

Upvotes: 1

Related Questions