2009-10-13 76 views
0

我有一个计划,将使用应用程序角色将数据写入到SQL Server 2005。应用程序角色连接到SQL Server与“登录失败”错误

using (SqlConnection sqlCon = new SqlConnection(connectionString)) 
{ 
    SqlCommand sqlCommand = new SqlCommand(); 
    sqlCommand.Connection = sqlCon; 
    sqlCommand.CommandType = CommandType.Text; 
    sqlCommand.CommandText = ""; 
    sqlCommand.CommandText = "EXEC sp_setapprole 'name','password';"; 
    sqlCommand.CommandText += sqlComm; 
    sqlCommand.CommandTimeout = 300; 
    sqlCon.Open(); 

    int res = sqlCommand.ExecuteNonQuery(); 
} 

我使用此代码连接到一个SQL Server 2005 ServerA,它运行良好。然后,我使用相同的代码连接anohter SQL Server 2005 ServerB,与相同的表,应用程序角色,它给出了一个错误。“登录失败的域\用户名”以前有人遇到过吗?

System.Data.SqlClient.SqlException是 未处理由用户代码
消息= “登录失败,用户 'DOMIAN \用户名'。”来源= “净 SqlClient数据提供程序。”
错误码= -2146232060类= 14
LineNumber上= 65536数= 18456
程序= “” 服务器= “服务器B”
状态= 1
堆栈跟踪:在
System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION 例外,布尔breakConnection)
在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand的cmdHandler, SqlDataReader的数据流, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
在System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔 enlistOK)
在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,字符串NEWPASSWORD, 布尔ignoreSniOpenTimeout,Int64类型 timerExpire,SqlConnection的 owningObject)
在System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(字符串 主机,串NEWPASSWORD,布尔 redirectedUserInstance,SqlConnection的 owningObject,SqlConnectionString connectionOptions,Int64的timerStart)
在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection的 owningObject,SqlConnectionString connectionOptions,串NEWPASSWORD, 布尔redirectedUserInstance)
在系统.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,Object providerInfo,String newPassword, SqlConnection owningObject,Boolean redirectedUserInstance)
在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,对象poolGroupProviderInfo, 池类DBConnectionPool,的DbConnection owningConnection)
在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(的DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory。的getConnection(的DbConnection owningConnection)
在System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory的)
在System.Data.SqlClient.SqlConnection.Open()
在ADSK.PSEB.ACRSubmitComponent。 ACRSubmit.backgroundUploadWork_DoWork(对象 发件人,DoWorkEventArgs e)如 C:\ Documents和 设置\ lvlu \桌面\穆德\上传\ ADSK.PSEB.ACRSubmitComponent \树干\ ADSK.PSEB.ACRSubmitComponent \ Form1.cs中:行 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventAr GS E)
在System.ComponentModel.BackgroundWorker.WorkerThreadStart(对象 参数)

回答

3

错误消息确实说明了一切:

SqlException was unhandled by user code 
Message="Login failed for user 'domain\username'." 
Source=".Net SqlClient Data Provider" ErrorCode=-2146232060 Class=14 

这个,你应该看到用户你用来连接到你的ServerB在第二台服务器上没有账号,显然。

它与您的应用程序角色或任何无关 - 您的用户“域\用户名”只是没有访问权限的第二台服务器。

为该用户添加一个登录名,并授予他对所需数据库的访问权限和必要权限,并且您应该没事。

马克

相关问题