SqlRoleProvider.DeleteRole(String, Boolean) Método
Definição
Remove uma função de banco de dados de função.Removes a role from the role database.
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
O nome da função a ser excluída.The name of the role to delete.
- throwOnPopulatedRole
- Boolean
Se true, gerará uma exceção se roleName tiver um ou mais membros.If true, throws an exception if roleName has one or more members.
Retornos
true se a função foi excluída com êxito; caso contrário, false.true if the role was successfully deleted; otherwise, false.
Exceções
roleName é null (Nothing no Visual Basic).roleName is null (Nothing in Visual Basic).
roleName é uma cadeia de caracteres vazia ou contém uma vírgula.roleName is an empty string or contains a comma.
- ou --or-
roleName tem mais de 256 caracteres.roleName is longer than 256 characters.
roleName tem um ou mais membros e throwOnPopulatedRole é true.roleName has one or more members and throwOnPopulatedRole is true.
- ou --or-
Erro desconhecido durante a comunicação com o banco de dados.An unknown error occurred while communicating with the database.
Exemplos
O exemplo a seguir exclui uma função do banco de dados.The following example deletes a role from the database. Para obter um exemplo de um arquivo de Web.config que habilita o gerenciamento de função, consulte SqlRoleProvider .For an example of a Web.config file that enables role management, see 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>
Comentários
O DeleteRole método é chamado pela Roles classe para excluir uma função do banco de dados SQL Server especificado no arquivo de configuração do aplicativo ASP.NET (Web.config).The DeleteRole method is called by the Roles class to delete a role from the SQL Server database specified in the ASP.NET application's configuration file (Web.config). Quando uma função é excluída, a lista de usuários associada a essa função também é excluída do banco de dados.When a role is deleted, the list of users associated with that role is also deleted from the database. As informações do usuário no banco de dados não são afetadas.The user information in the database is not affected.
Se throwOnPopulatedRole for true , uma exceção será lançada e a função não será excluída se a função identificada pelo roleName parâmetro tiver um ou mais membros.If throwOnPopulatedRole is true, then an exception will be thrown and the role will not be deleted if the role identified by the roleName parameter has one or more members. Se throwOnPopulatedRole for false , a função será excluída se estiver vazia ou não.If throwOnPopulatedRole is false, then the role will be deleted whether it is empty or not.