SqlRoleProvider.DeleteRole(String, Boolean) Método

Definición

Quita un rol de la base de datos de roles.

public:
 override bool DeleteRole(System::String ^ roleName, bool throwOnPopulatedRole);
public override bool DeleteRole (string roleName, bool throwOnPopulatedRole);
override this.DeleteRole : string * bool -> bool
Public Overrides Function DeleteRole (roleName As String, throwOnPopulatedRole As Boolean) As Boolean

Parámetros

roleName
String

Nombre del rol que se va a eliminar.

throwOnPopulatedRole
Boolean

Si es true, se produce una excepción si roleName tiene uno o más miembros.

Devoluciones

Boolean

true si el rol se eliminó correctamente; de lo contrario, false.

Excepciones

roleName es null (Nothing en Visual Basic).

roleName es una cadena vacía o contiene una coma.

o bien

roleName tiene más de 256 caracteres.

roleName tiene uno o más miembros y throwOnPopulatedRole es true.

o bien

Se ha producido un error desconocido al establecer comunicación con la base de datos.

Ejemplos

En el ejemplo siguiente se elimina un rol de la base de datos. Para obtener un ejemplo de un archivo Web.config que habilita la administración de roles, consulte SqlRoleProvider.


<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  string[] rolesArray;

  public void Page_Load(object sender, EventArgs args)
  {
    if (!IsPostBack)
    {
      // Bind roles to ListBox.

      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      RolesListBox.DataBind();
    }
  }


  public void DeleteRole_OnClick(object sender, EventArgs args)
  {
    string delRole = "";

    try
    {
      delRole = RolesListBox.SelectedItem.Value;

      Roles.DeleteRole(delRole);

      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' deleted.";


      // Re-bind roles to ListBox.

      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      RolesListBox.DataBind();
    }
    catch
    {
      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' <u>not</u> deleted.";
    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Delete Role</title>
</head>
<body>
  <form runat="server" id="PageForm">
    <h3>
      Delete Role</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table border="0">
      <tr>
        <td valign="top">
          Delete Role:</td>
        <td valign="top">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td>
        <td valign="top">
          <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td>
      </tr>
    </table>
  </form>
</body>
</html>

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Dim rolesArray() As String

  Public Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs)

    If Not IsPostBack Then
      ' Bind roles to ListBox.

      rolesArray = Roles.GetAllRoles()
      RolesListBox.DataSource = rolesArray
      RolesListBox.DataBind()
    End If

  End Sub


  Public Sub DeleteRole_OnClick(ByVal sender As Object, ByVal args As EventArgs)

    Dim delRole As String

    Try
      delRole = RolesListBox.SelectedItem.Value

      Roles.DeleteRole(delRole)

      Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' deleted."

      ' Re-bind roles to ListBox.

      rolesArray = Roles.GetAllRoles()
      RolesListBox.DataSource = rolesArray
      RolesListBox.DataBind()
    Catch
      Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' <u>not</u> deleted."
    End Try

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Delete Role</title>
</head>
<body>
  <form runat="server" id="PageForm">
    <h3>
      Delete Role</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table border="0">
      <tr>
        <td valign="top">
          Delete Role:</td>
        <td valign="top">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td>
        <td valign="top">
          <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td>
      </tr>
    </table>
  </form>
</body>
</html>

Comentarios

La DeleteRole clase llama Roles al método para eliminar un rol de la base de datos de SQL Server especificada en el archivo de configuración de la aplicación ASP.NET (Web.config). Cuando se elimina un rol, la lista de usuarios asociados a ese rol también se elimina de la base de datos. La información del usuario de la base de datos no se ve afectada.

Si throwOnPopulatedRole es true, se producirá una excepción y el rol no se eliminará si el rol identificado por el roleName parámetro tiene uno o varios miembros. Si throwOnPopulatedRole es false, el rol se eliminará si está vacío o no.

Se aplica a

Consulte también