SqlRoleProvider.DeleteRole(String, Boolean) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從角色資料庫中移除角色。
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
。
例外狀況
roleName
為 null
(在 Visual Basic 中為 Nothing
)。
範例
下列範例會從資料庫刪除角色。 如需啟用角色管理的 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 資料庫中刪除角色。 刪除角色時,也會從資料庫刪除與該角色相關聯的使用者清單。 資料庫中的使用者資訊不會受到影響。
如果 為 throwOnPopulatedRole
true
,則會擲回例外狀況,而且如果參數識別 roleName
的角色具有一或多個成員,則不會刪除該角色。 如果 throwOnPopulatedRole
為 false
,則不論該角色是否為空白,都會刪除該角色。