SqlRoleProvider.DeleteRole(String, Boolean) 方法

定义

从角色数据库移除一个角色。

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

参数

roleName
String

要删除的角色的名称。

throwOnPopulatedRole
Boolean

如果为 true,则当 roleName 包含一个或多个成员时将引发异常。

返回

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

例外

roleNamenull(在 Visual Basic 中为 Nothing)。

roleName 为空字符串或者包含逗号。

- 或 -

roleName 的长度超过 256 个字符。

roleName 具有一个或多个成员且 throwOnPopulatedRoletrue

- 或 -

与数据库进行通信时发生未知错误。

示例

以下示例从数据库中删除角色。 有关启用角色管理的 Web.config 文件的示例,请参阅 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>

注解

DeleteRole方法由 Roles 类调用,以从 ASP.NET 应用程序的配置文件 (Web.config) 中指定的 SQL Server 数据库中删除角色。 删除角色时,也会从数据库中删除与该角色关联的用户列表。 数据库中的用户信息不受影响。

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

适用于

另请参阅