RoleProvider.DeleteRole(String, Boolean) 方法

定义

从数据源中删除已配置的 applicationName 的一个角色。

public:
 abstract bool DeleteRole(System::String ^ roleName, bool throwOnPopulatedRole);
public abstract bool DeleteRole (string roleName, bool throwOnPopulatedRole);
abstract member DeleteRole : string * bool -> bool
Public MustOverride Function DeleteRole (roleName As String, throwOnPopulatedRole As Boolean) As Boolean

参数

roleName
String

要删除的角色的名称。

throwOnPopulatedRole
Boolean

若为 true,则在 roleName 具有一个或多个成员时引发异常,且不删除 roleName

返回

若已成功删除此角色,则为 true;否则为 false

示例

下面的代码示例演示 方法的示例 DeleteRole 实现。

public override bool DeleteRole(string rolename, bool throwOnPopulatedRole)
{
  if (!RoleExists(rolename))
  {
    throw new ProviderException("Role does not exist.");
  }

  if (throwOnPopulatedRole && GetUsersInRole(rolename).Length > 0)
  {
    throw new ProviderException("Cannot delete a populated role.");
  }

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("DELETE FROM Roles "  +
                                    " WHERE Rolename = ? AND ApplicationName = ?", conn);

  cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename;
  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;

  OdbcCommand cmd2 = new OdbcCommand("DELETE FROM UsersInRoles "  +
                                     " WHERE Rolename = ? AND ApplicationName = ?", conn);

  cmd2.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename;
  cmd2.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;

  try
  {
    conn.Open();

    cmd2.ExecuteNonQuery();
    cmd.ExecuteNonQuery();
  }
  catch (OdbcException)
  {
    // Handle exception.

    return false;
  }
  finally
  {
    conn.Close();      
  }

  return true;
}
Public Overrides Function DeleteRole(ByVal rolename As String, ByVal throwOnPopulatedRole As Boolean) As Boolean
    If Not RoleExists(rolename) Then
        Throw New ProviderException("Role does not exist.")
    End If

    If throwOnPopulatedRole AndAlso GetUsersInRole(rolename).Length > 0 Then
        Throw New ProviderException("Cannot delete a populated role.")
    End If

    Dim conn As OdbcConnection = New OdbcConnection(connectionString)
    Dim cmd As OdbcCommand = New OdbcCommand("DELETE FROM Roles " & _
                                             " WHERE Rolename = ? AND ApplicationName = ?", conn)

    cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename
    cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName

    Dim cmd2 As OdbcCommand = New OdbcCommand("DELETE FROM UsersInRoles " & _
                                              " WHERE Rolename = ? AND ApplicationName = ?", conn)

    cmd2.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename
    cmd2.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName

    Try
        conn.Open()

        cmd2.ExecuteNonQuery()
        cmd.ExecuteNonQuery()
    Catch e As OdbcException
        ' Handle exception.

        Return False
    Finally
        conn.Close()
    End Try

    Return True
End Function

注解

DeleteRoleDeleteRole 类的 DeleteRoleRoles 和 方法调用,以从配置的 ApplicationName的数据源中删除指定角色。

从数据源中删除角色时,请确保同时删除配置的 applicationName的用户名与已删除角色之间的任何关联。

如果 throwOnPopulatedRoletrue,并且参数标识 roleName 的角色具有一个或多个成员,则引发 ProviderException 并不删除该角色。 如果 throwOnPopulatedRolefalse,则无论角色是否为空,都将其删除。

如果指定的角色名称不存在、为 null或为空字符串,建议提供程序引发异常。

适用于

另请参阅