简体   繁体   English

连接SQL Server的程序中的应用程序角色

[英]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. 我有一个程序,该程序将使用“应用程序角色”将数据写入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 事件ID 18059源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. 默认情况下处于启用状态,并且连接池不适用于不可模仿的假冒,如approls。

The using{} statement calls IDispose at the end of the block thus killing your connection. using {}语句在该块的末尾调用IDispose,从而终止了连接。 https://msdn.microsoft.com/en-us/library/yh598w02.aspx using Statement (C# Reference) https://msdn.microsoft.com/zh-cn/library/yh598w02.aspx使用Statement(C#参考)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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