gpt4 book ai didi

c# - 连接到 SQL 数据库时出错 : Cannot open database "Students.mdf" requested by the login. 登录失败

转载 作者:行者123 更新时间:2023-11-30 20:45:35 25 4
gpt4 key购买 nike

我真的很沮丧,这很简单但对我不起作用。我正在尝试连接到我的 SQL Server,在运行 SSMS 时我不需要用户名和密码。我的服务器名称是 HP\SQLEXPRESS,我使用以下连接字符串:

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

我收到以下错误:

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

当我如下指定我的数据库的完整地址时:

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 connection string

关于c# - 连接到 SQL 数据库时出错 : Cannot open database "Students.mdf" requested by the login. 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27996937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com