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 (Nothing 在 Visual Basic 中)。
範例
以下範例是從資料庫中刪除一個角色。 關於啟用角色管理的 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,則無論該角色是否空,該角色都會被刪除。