应用程序角色连接到SQL Server时出现“登录失败”错误

时间:2009-10-13 05:06:49

标签: .net sql-server winforms

我有一个程序可以使用Application Role将数据写入SQL Server 2005。

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 \ username'。“Source =”。Net   SqlClient数据提供者“
  ErrorCode = -2146232060等级= 14
  LineNumber = 65536 Number = 18456
  Procedure =“”Server =“ServerB”
  国家= 1个
  堆栈跟踪:
    在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException   exception,Boolean breakConnection)
    在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject)   stateObj)
    在System.Data.SqlClient.TdsParser.Run(RunBehavior   runBehavior,SqlCommand cmdHandler,   SqlDataReader dataStream,   BulkCopySimpleResultSet   bulkCopyHandler,TdsParserStateObject   stateObj)
    在System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔值   enlistOK)
    在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo   serverInfo,String newPassword,   Boolean ignoreSniOpenTimeout,Int64   timerExpire,SqlConnection   owningObject)
    在System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String   host,String newPassword,Boolean   redirectedUserInstance,SqlConnection   owningObject,SqlConnectionString   connectionOptions,Int64 timerStart)
    在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection   owningObject,SqlConnectionString   connectionOptions,String newPassword,   Boolean redirectedUserInstance)
    在System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity   identity,SqlConnectionString   connectionOptions,Object   providerInfo,String newPassword,   SqlConnection owningObject,Boolean   redirectedUserInstance)
     在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions   options,Object poolGroupProviderInfo,   DbConnectionPool池,DbConnection   owningConnection)
     在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection)   owningConnection,   DbConnectionPoolGroup poolGroup)
     在System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection   owningConnection)
     在System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection   outerConnection,DbConnectionFactory   的connectionFactory)
     在System.Data.SqlClient.SqlConnection.Open()
     at ADSK.PSEB.ACRSubmitComponent.ACRSubmit.backgroundUploadWork_DoWork(Object   发件人,DoWorkEventArgs e)in   C:\ Documents and   设置\ lvlu \桌面\穆德\上传\ ADSK.PSEB.ACRSubmitComponent \干线\ ADSK.PSEB.ACRSubmitComponent \ Form1.cs中:行   897个
     在System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs   E)
     在System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object   论证)

2 个答案:

答案 0 :(得分:3)

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

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

从这一点来看,您应该看到用于连接到ServerB的用户显然没有第二台服务器上的帐户。

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

为该用户添加登录信息,并授予他访问权限和所需数据库的必要权限,您应该没问题。

马克

答案 1 :(得分:1)

你的连接线可能有问题

connectionstring的格式如下:

<add name="dbconnect" connectionString="Data Source=localhost;Initial Catalog=db_name;User ID=sa;Password=....." providerName="System.Data.SqlClient" />

参考网站:Connection problems with SQL Server in ASP.NET applications using out-of-process session state