RoleProvider.DeleteRole(String, Boolean) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為已設定的 applicationName
移除資料來源中的角色。
public:
abstract bool DeleteRole(System::String ^ roleName, bool throwOnPopulatedRole);
public abstract bool DeleteRole (string roleName, bool throwOnPopulatedRole);
abstract member DeleteRole : string * bool -> bool
Public MustOverride Function DeleteRole (roleName As String, throwOnPopulatedRole As Boolean) As Boolean
參數
- roleName
- String
要刪除的角色名稱。
- throwOnPopulatedRole
- Boolean
如果 true
,且如果 roleName
有一或多個成員則擲回例外狀況,且不刪除 roleName
。
傳回
如果成功刪除角色,即為 true
;否則為 false
。
範例
下列程式代碼範例示範 方法的範例實作 DeleteRole 。
public override bool DeleteRole(string rolename, bool throwOnPopulatedRole)
{
if (!RoleExists(rolename))
{
throw new ProviderException("Role does not exist.");
}
if (throwOnPopulatedRole && GetUsersInRole(rolename).Length > 0)
{
throw new ProviderException("Cannot delete a populated role.");
}
OdbcConnection conn = new OdbcConnection(connectionString);
OdbcCommand cmd = new OdbcCommand("DELETE FROM Roles " +
" WHERE Rolename = ? AND ApplicationName = ?", conn);
cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename;
cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;
OdbcCommand cmd2 = new OdbcCommand("DELETE FROM UsersInRoles " +
" WHERE Rolename = ? AND ApplicationName = ?", conn);
cmd2.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename;
cmd2.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;
try
{
conn.Open();
cmd2.ExecuteNonQuery();
cmd.ExecuteNonQuery();
}
catch (OdbcException)
{
// Handle exception.
return false;
}
finally
{
conn.Close();
}
return true;
}
Public Overrides Function DeleteRole(ByVal rolename As String, ByVal throwOnPopulatedRole As Boolean) As Boolean
If Not RoleExists(rolename) Then
Throw New ProviderException("Role does not exist.")
End If
If throwOnPopulatedRole AndAlso GetUsersInRole(rolename).Length > 0 Then
Throw New ProviderException("Cannot delete a populated role.")
End If
Dim conn As OdbcConnection = New OdbcConnection(connectionString)
Dim cmd As OdbcCommand = New OdbcCommand("DELETE FROM Roles " & _
" WHERE Rolename = ? AND ApplicationName = ?", conn)
cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename
cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName
Dim cmd2 As OdbcCommand = New OdbcCommand("DELETE FROM UsersInRoles " & _
" WHERE Rolename = ? AND ApplicationName = ?", conn)
cmd2.Parameters.Add("@Rolename", OdbcType.VarChar, 255).Value = rolename
cmd2.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName
Try
conn.Open()
cmd2.ExecuteNonQuery()
cmd.ExecuteNonQuery()
Catch e As OdbcException
' Handle exception.
Return False
Finally
conn.Close()
End Try
Return True
End Function
備註
DeleteRole由 DeleteRole 類別的 Roles 和 DeleteRole 方法呼叫,以從所設定 ApplicationName的數據源中刪除指定的角色。
當您從資料來源中刪除角色時,請確定您也會刪除使用者名稱與所設定 applicationName
已刪除角色之間的任何關聯。
如果 為 true
throwOnPopulatedRole
,且 參數所roleName
識別的角色有一或多個成員,請擲回 ProviderException ,且不會刪除角色。 如果 throwOnPopulatedRole
為 false
,則刪除角色是否空白。
如果指定的角色名稱不存在、為 null
或 是空字串,建議您的提供者擲回例外狀況。