共用方式為


RoleProvider.DeleteRole(String, Boolean) 方法

定義

為已設定的 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

備註

DeleteRoleDeleteRole 類別的 RolesDeleteRole 方法呼叫,以從所設定 ApplicationName的數據源中刪除指定的角色。

當您從資料來源中刪除角色時,請確定您也會刪除使用者名稱與所設定 applicationName已刪除角色之間的任何關聯。

如果 為 truethrowOnPopulatedRole ,且 參數所roleName識別的角色有一或多個成員,請擲回 ProviderException ,且不會刪除角色。 如果 throwOnPopulatedRolefalse,則刪除角色是否空白。

如果指定的角色名稱不存在、為 null或 是空字串,建議您的提供者擲回例外狀況。

適用於

另請參閱