[英]SqlException: Cannot open database <database> requested by the login. The login failed
[英]Error when connecting to a SQL database: Cannot open database “Students.mdf” requested by the login. The login failed
我真的很沮喪,這很簡單,但是沒有和我一起工作。 我正在嘗試連接到運行SSMS時不需要用戶名和密碼的SQL Server。 我的服務器名稱是HP\\SQLEXPRESS
並且正在使用以下連接字符串:
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students.mdf;Integrated Security=SSPI;";
我收到以下錯誤:
System.Data.dll中發生了類型為'System.Data.SqlClient.SqlException'的未處理的異常
附加信息:無法打開登錄請求的數據庫“ Students.mdf”。 登錄失敗。
用戶“ HP \\ Aymen”的登錄失敗。
當我按如下方式指定數據庫的完整地址時:
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=c:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLEXPRESS\\MSSQL\\DATA\\Students.mdf;Integrated Security=SSPI;";
這也給了我同樣的錯誤。
有什么建議嗎?
我相信您對該用戶有權限問題。
您是否嘗試過與其他用戶(管理員用戶)登錄到該數據庫?
從SSMS連接時,您具有管理員權限。 我認為運行您的程序的用戶並非如此。
但是,我建議您使用create login創建一個特定的用戶,然后只需將參數添加到sql server連接字符串中
嘗試使用sa登錄名訪問數據庫
User ID=sa;
Password="your password without quotes";
和
data source=.
在您的連接字符串中
對於WINDOWS Auth。
string connectionString = "Data Source=.;Initial Catalog=Students;Integrated Security=SSPI;"
對於SQL AUTH。
string connectionString="Data Source=.;User ID=sa;
Password=; Initial Catalog=Students; " providerName="System.Data.SqlClient";
嘗試這個
我認為您正在使用Windows身份驗證
string connectionString = "Data Source=HP\\SQLEXPRESS;
Initial Catalog=Students;Integrated Security=SSPI;"
似乎是身份驗證問題。 您還應該在連接字符串中指定用戶ID。
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students;Integrated Security=SSPI;User ID=PCName\WindowsUser;";
如果仍然無法使用,請嘗試此操作。
string connectionString = "Server=HP\\SQLEXPRESS;Database=Students;Trusted_Connection=True;
問題解決了。 看來問題出在因為我已經使用實體框架模型建立了到數據庫的連接。 當我刪除它並再次運行該應用程序時,一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.