简体   繁体   English

SqlException:无法打开登录请求的数据库“ path \\ db.mdf”。 登录失败。 用户“ NT AUTHORITY \\ SYSTEM”的登录失败

[英]SqlException: Cannot open database “path\db.mdf” requested by the login. The login failed. Login failed for user 'NT AUTHORITY\SYSTEM'

I know that this question have been already asked many times but I looked through the answers and haven't found solution for my problem. 我知道这个问题已经被问过很多次了,但是我仔细查看了答案,却没有找到解决问题的方法。

I've built a Windows Service in C# that connects to the database and fill it with data from some other source. 我用C#构建了Windows Service ,该Windows Service连接到数据库并用其他来源的数据填充它。 I set Account -> LocalSystem for ServiceProcessInstaller and StartType -> Automatic for ServiceInstaller . 我为ServiceProcessInstaller StartType -> Automatic Account -> LocalSystem ,为ServiceInstaller StartType -> Automatic

I use Entity Framework to generate .edmx from database, this is a connection string: 我使用实体框架从数据库生成.edmx,这是一个连接字符串:

<add name="ConStringName"
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
     provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;
     attachdbfilename=&quot;somePath\MyDbName.mdf&quot;;
     integrated security=True;connect timeout=30;
     MultipleActiveResultSets=True;
     App=EntityFramework'"
         providerName="System.Data.EntityClient" />

And I have this code: 我有这段代码:

var objContext = ((IObjectContextAdapter)dbContext).ObjectContext;
objContext.ExecuteStoreCommand("DELETE FROM Properties;");

On the second line I get 在第二行,我得到

Cannot open database "path\DbName.MDF" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\SYSTEM'.

What I've tried so far: 到目前为止,我已经尝试过:

1) tried this code for each database table: 1)对每个数据库表尝试以下代码:

GRANT SELECT, INSERT, UPDATE, DELETE ON  dbo.TableName TO "NT AUTHORITY\SYSTEM";

and without dbo. 而且没有dbo。

 GRANT SELECT, INSERT, UPDATE, DELETE ON  TableName TO "NT AUTHORITY\SYSTEM";

2) I have set the permissions for this user 'NT AUTHORITY\\SYSTEM' in my database: 2)我已经在数据库中为此用户“ NT AUTHORITY \\ SYSTEM”设置了权限:
In SSMS: Databases => myDatabase -> Security -> Users -> 'NT AUTHORITY\\SYSTEM' -> Properties -> Membership -> db_owner 在SSMS中:数据库=> myDatabase->安全->用户->'NT AUTHORITY \\ SYSTEM'->属性->成员资格-> db_owner

3) Tried to set perissions for this account in SQL Server: Databases -> Security -> Logins -> 'NT AUTHORITY\\SYSTEM' -> Properties -> set db_owner here 3)试图在SQL Server中为此帐户设置权限:数据库->安全->登录->'NT AUTHORITY \\ SYSTEM'->属性->在此处设置db_owner

The weird thing: One time I have restarted the computer and everything was working!! 奇怪的是:有一次我重新启动计算机,一切正常! Like this LocalSystem had permissions and the Windows Service could access the database and perform all the operations! 像这样,LocalSystem拥有权限,Windows Service可以访问数据库并执行所有操作!

But than after some circumstances this exception started to appear again. 但是比起某些情况之后,此异常开始再次出现。 I've tried to restart the computer but failed. 我试图重新启动计算机,但是失败了。

I know I'm missing something obvious. 我知道我缺少明显的东西。 Please, help me find this thing. 请帮我找到这个东西。

Rather then trying to set permission to your database to local system account, it's better to do one of the following: 与其尝试为本地系统帐户设置对数据库的权限,不如执行以下操作之一:

  1. Change the account that runs the windows service to a Windows account that already have a permission in the database. 将运行Windows服务的帐户更改为数据库中已具有权限的Windows帐户。

  2. Don't use integrated security=True in your connection string but specify a User Id and Password . 不要在您的连接字符串中使用integrated security=True ,而是指定一个User IdPassword

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法打开登录请求的数据库。 登录失败。 用户“ NT AUTHORITY \\ SYSTEM”的登录失败 - Cannot open database requested by the login. The login failed. Login failed for user 'NT AUTHORITY\SYSTEM' LocalDb-&gt; Windows服务-&gt;无法打开登录请求的数据库“”。 登录失败。 用户“ NT AUTHORITY \\ LOCAL SERVICE”的登录失败 - LocalDb -> Windows Service -> Cannot open database “” requested by the login. The login failed. Login failed for user 'NT AUTHORITY\LOCAL SERVICE' 无法打开登录请求的数据库。 用户“NT AUTHORITY\\SYSTEM”登录失败。 Quartz 调度程序 Windows 服务 - Cannot open database requested by the login. Login failed for user 'NT AUTHORITY\SYSTEM'. Quartz Scheduler Windows Services 无法打开登录请求的数据库“ [数据库]”。 登录失败。 用户“ [User]”的登录失败 - Cannot open database “[Database]” requested by the login. The login failed. Login failed for user '[User]" 用户&#39;&#39;登录失败,无法打开登录请求的数据库“Database1.mdf”。 登录失败。 用户&#39;rBcollo-PC \\ rBcollo&#39;登录失败 - Login failed for user '' and Cannot open database “Database1.mdf” requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo' 无法打开登录请求的数据库“身份”。 登录失败。 用户“DESKTOP\User”登录失败 - Cannot open database “Identity” requested by the login. The login failed. Login failed for user 'DESKTOP\User' 无法打开登录请求的数据库“MyBase”。 登录失败。 用户“域\用户”登录失败 - Cannot open database "MyBase" requested by the login. The login failed. Login failed for user 'domain\User' 无法打开登录请求的数据库“ Inventory_New”。 登录失败。 用户“ sa”的登录失败 - Cannot open database “Inventory_New” requested by the login. The login failed. Login failed for user 'sa' 无法打开登录请求的数据库“ ASPState”。 登录失败。 用户“ xxxx”的登录失败 - Cannot open database “ASPState” requested by the login. The login failed. Login failed for user 'xxxx' 无法打开登录请求的数据库“test”。 登录失败。 用户 &#39;Domain\\userid&#39; 登录失败 - Cannot open database "test" requested by the login. The login failed. Login failed for user 'Domain\userid'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM