[英]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="somePath\MyDbName.mdf";
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: 与其尝试为本地系统帐户设置对数据库的权限,不如执行以下操作之一:
Change the account that runs the windows service to a Windows account that already have a permission in the database. 将运行Windows服务的帐户更改为数据库中已具有权限的Windows帐户。
Don't use integrated security=True
in your connection string but specify a User Id
and Password
. 不要在您的连接字符串中使用
integrated security=True
,而是指定一个User Id
和Password
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.