Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 关闭连接后重置SQL应用程序角色_Sql Server_Sql Server 2008 R2_Application Role - Fatal编程技术网

Sql server 关闭连接后重置SQL应用程序角色

Sql server 关闭连接后重置SQL应用程序角色,sql-server,sql-server-2008-r2,application-role,Sql Server,Sql Server 2008 R2,Application Role,我正在使用.net应用程序中的sql应用程序角色。我有一个问题发生在连接断开时。例如,我有一段代码,它打开一个连接,设置应用程序角色,从数据库中进行选择,并处理我的连接。如果我第二次运行此代码,则在尝试再次设置应用程序角色时失败(sys.sp_setapprole的ExecuteNonQuery()行) 异常是SqlException:当前命令发生严重错误。如果有结果,则应放弃 我尝试过使用@fCreateCookie参数并调用sys.sp_unsetapprole来重置角色,但这没有什么区别

我正在使用.net应用程序中的sql应用程序角色。我有一个问题发生在连接断开时。例如,我有一段代码,它打开一个连接,设置应用程序角色,从数据库中进行选择,并处理我的连接。如果我第二次运行此代码,则在尝试再次设置应用程序角色时失败(sys.sp_setapprole的ExecuteNonQuery()行)

异常是SqlException:当前命令发生严重错误。如果有结果,则应放弃

我尝试过使用@fCreateCookie参数并调用sys.sp_unsetapprole来重置角色,但这没有什么区别

请帮忙

using (SqlConnection connection = new SqlConnection(myConnectionString))
        {
            connection.Open();

            using (SqlCommand command = new SqlCommand("sys.sp_setapprole", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@rolename", "MyRole");
                command.Parameters.AddWithValue("@password", "MyPassword");

                command.ExecuteNonQuery();
            }

            try
            {
                DataSet ds = new DataSet();

                using (SqlCommand command = new SqlCommand("dbo.MyProcedure", connection))
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    adapter.Fill(ds);                        
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

您使用的是什么版本的SQL Server?此外,我没有看到关闭和处理连接的代码。你在用什么平台?C#?