简体   繁体   中英

Error when connecting to a SQL database: Cannot open database “Students.mdf” requested by the login. The login failed

I am really frustrated, this is very simple but didn't work with me. I'm trying to connect to my SQL Server which I don't need username and password when running SSMS. My server name is HP\\SQLEXPRESS and I'm using the following connection string:

string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students.mdf;Integrated Security=SSPI;";

and I get the following error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Cannot open database "Students.mdf" requested by the login. The login failed.

Login failed for user 'HP\\Aymen'.

And when I specify the full address of my database as follows:

string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=c:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLEXPRESS\\MSSQL\\DATA\\Students.mdf;Integrated Security=SSPI;";

it also gives me the same error.

Any suggestion?

I believe you have permissions problem for that user.

have you tried login with other user (admin user) to that db?

  1. use your admin user to login to ssms.
  2. under security, change the server Roles and user mapping to that specific user you want, and try to login again. do forget to give that user permission to "master".

when you're connecting from the SSMS, you have admin privileges. i think this is not the case of the user that run your program.

however, i would suggest you to create a specific user using create login then, simply just add your parameters to sql server connection string

Try using sa login to access the database

User ID=sa;
Password="your password without quotes"; 

and

data source=.

in your connection string

UPDATED CONNECTION STRING

FOR WINDOWS Auth.

string connectionString = "Data Source=.;Initial Catalog=Students;Integrated Security=SSPI;"

FOR SQL AUTH.

string connectionString="Data Source=.;User ID=sa;
Password=; Initial Catalog=Students; " providerName="System.Data.SqlClient";

try this

I think Your are using Windows Authentication

string connectionString = "Data Source=HP\\SQLEXPRESS;
Initial Catalog=Students;Integrated Security=SSPI;" 

seems authentication issue here. you should specify the user id too in the connection string.

string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students;Integrated Security=SSPI;User ID=PCName\WindowsUser;";

If still doesn't work then try this also.

string connectionString = "Server=HP\\SQLEXPRESS;Database=Students;Trusted_Connection=True;

The problem solved. It seems that the problem is because I already have made a connection to the database using Entity Framework model. When I removed it and run the application again everything went fine.

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