简体   繁体   中英

Application Role in Program connecting the SQL Server

I have a program which would use the Application Role to write data to a SQL Server 2005.

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

    sqlCon.Open();
    int res = sqlCommand.ExecuteNonQuery();
}

This code is in a loop. for the first time, it's OK. In second iterator, it throws exception.

The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context. This scenario is not supported. See "Impersonation Overview" in Books Online. Event ID 18059 Source MSSQLSERVER

Is there anyone meet this problem before?

Best Regards,

turn off connection pooling, explicitly. by default is on and connection pooling does not work with unreversible impersonation like approles.

The using{} statement calls IDispose at the end of the block thus killing your connection. https://msdn.microsoft.com/en-us/library/yh598w02.aspx using Statement (C# Reference)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM