簡體   English   中英

嘗試從Windows Service訪問數據庫時出錯

[英]Error trying to access database from Windows Service

我希望從Windows服務訪問數據庫,為此我在解決方案中有幾個項目。 Win服務本身會啟動一個計時器,該計時器每X秒執行一次操作。 此操作位於類庫(業務)中,該類庫又可以訪問訪問數據庫的DAL層(在另一個庫類中)。

我通常使用這種方案來訪問數據庫中的數據,在這種特殊情況下,例外的是,我的用戶界面是Windows服務。 該服務有一個app.config文件,您可以在其中定義數據庫的連接字符串。

我已經在另一個項目(Winforms)中驗證了連接字符串是否有效(它從數據庫返回數據)。 對於服務的安裝,請在相同的解決方案中生成一個安裝程序。

所有這些編譯都沒有問題。 並且該服務已正確安裝,但是當我啟動時,它告訴我計時器每次在您使用上述方法進入數據庫時​​都在運行。

我的問題是,當我訪問時,收到一條消息,告訴我無法訪問數據庫。 這是消息

System.Data.DataException:初始化數據庫時發生異常。 有關詳細信息,請參見InnerException。

System.Data.Entity.Core.EntityException:基礎提供程序在打開時失敗。

System.Data.SqlClient.SqlException:沒有適當的基礎知識可以從“ CertusAccuro_dev”中獲取信息。 錯誤的設置。
使用權限錯誤“ NT AUTHORITY \\ SYSTEM”。

烯System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,UInt32的waitForMultipleObjectsTimeout,布爾allowCreate,布爾onlyOneCheckConnection,DbConnectionOptions USEROPTIONS,DbConnectionInternal&連接)連接System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重試,DbConnectionOptions USEROPTIONS,DbConnectionInternal oldConnection,DbConnectionInternal&連接)連接System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(的DbConnection outerConnection,DbConnectionFactory connectionFactory的,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重試,DbConnectionOptions userOptions)zh-cn System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1重試)zh- 1 retry) en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource System.Data.SqlClient.SqlConnection.Open( )zh_cn System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t,DbConnectionInterceptionContext c)zh- 1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action 2操作, TInterceptionContext InterceptionContext, 3 executing, Action動作3 executing, Action 3)zh-CN System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection連接,DbInterceptionContext攔截上下文)en System.Data.Entity.Core.EntityClient.EntityConnection.b__2()zh-CN .Data.Entity.SqlServer.DefaultSqlExecutionStrategy。<> c__DisplayClass1.b__0()zh-cn System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult] (Func 1 operation) en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) en System.Data.Entity.Core.EntityClient.EntityConnection.Open() --- Fin del seguimiento de la pila de la excepción interna --- en System.Data.Entity.Core.EntityClient.EntityConnection.Open() en System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions) en System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1,IDbExecutionStrategy executeStrategy,布爾型startLocalTransaction,布爾型releaseConnectionOnSuccess),System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1操作)zh- 1.GetResults(Nullable System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable null的1 forMergeOption)zh-CN System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() en System.Data.Entity.Internal.LazyEnumerator 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() en System.Data.Entity.Internal.LazyEnumerator 1.MoveNext()zh-cn System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable 1 source) en System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable 1個序列)zh_cn System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult](IEnumerable 1 query, Expression queryRoot) en System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) en System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) en System.Linq.Queryable.FirstOrDefault[TSource](IQueryable 1源)zh-CN .Internal.EdmMetadataRepository.QueryForModelHash(Func`2 createContext)zh_cn System.Data.Entity.Internal.InternalContext.QueryForModelHash()zh_CN System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext,ModelHashCalculator modelHashCalculator,Boolean throwIfNoMetadata,

錯誤翻譯為英文意味着:

用戶登錄錯誤“ NT AUTHORITY \\ SYSTEM”。

Windows服務中app.config的使用是否正確? 還是有另一種方法來訪問連接字符串以訪問數據庫?

請幫忙,我對這個話題有些迷茫。

提前致謝

錯誤翻譯為英文意味着:

用戶登錄錯誤“ NT AUTHORITY \\ SYSTEM”。

否,翻譯成英文的錯誤是

用戶“ NT AUTHORITY \\ SYSTEM”的登錄失敗。 原因:無法打開顯式指定的數據庫'CertusAccuro_dev'

這意味着您的登錄名NT AUTHORITY\\SYSTEM未映射到該數據庫,或者該數據庫不是ONLINE

第一個原因似乎是最有可能的,因此要解決此問題,您應該以sysadmin身份連接到服務器並執行以下代碼:

use CertusAccuro_dev;
go

create user [NT AUTHORITY\SYSTEM] from login [NT AUTHORITY\SYSTEM];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM