我正在开发一个Windows服务,用于将数据从一个远程数据库传输到另一个数据库。
当我尝试连接到数据库时,我收到以下错误。此连接字符串在Web应用程序中运行良好。
仅当我尝试使用Windows服务连接数据库时才会出现此错误。
System.Data.SqlClient.SqlException: Login failed for user 'sa'. Reason: Not associated with a trusted SQL Server connection.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at WindowsService.Program.timer1_Elapsed(Object sender, EventArgs e) in E:\Trigger\WindowsService\WindowsService\Program.cs:line 93
我正在使用的Connectionstring:
SqlConnection con = new SqlConnection("Data Source=TAG2;Initial Catalog=A10;User ID=sa; pwd=root;Integrated Security=False");"
答案 0 :(得分:3)
sql server很可能设置为“仅Windows身份验证”模式。 请参阅此MSDN link以解决您的错误。
答案 1 :(得分:1)
作为默认安装,我认为,它设置为仅允许Windows身份验证。但是,使用Sql Server Management Studio(如果使用),您可以启用Sql Server身份验证。点击此处的链接http://cherupally.blogspot.com.au/2009/04/user-is-not-associated-with-trusted-sql.html
答案 2 :(得分:0)
尝试检查数据库上的设置 - 您可能需要更改身份验证方法 “仅限Windows”到“SQL身份验证”
您可以阅读更多here