简体   繁体   中英

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\u0026#39;."

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...

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM