Rediger

Del via


RoleProvider.DeleteRole(String, Boolean) Method

Definition

Removes a role from the data source for the configured 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

Parameters

roleName
String

The name of the role to delete.

throwOnPopulatedRole
Boolean

If true, throw an exception if roleName has one or more members and do not delete roleName.

Returns

true if the role was successfully deleted; otherwise, false.

Examples

The following code example shows a sample implementation of the DeleteRole method.

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

Remarks

DeleteRole is called by the DeleteRole and the DeleteRole methods of the Roles class to delete the specified role from the data source for the configured ApplicationName.

When you delete a role from the data source, ensure that you also delete any associations between a user name and the deleted role for the configured applicationName.

If throwOnPopulatedRole is true, and the role identified by the roleName parameter has one or more members, throw a ProviderException and do not delete the role. If throwOnPopulatedRole is false, then delete the role whether it is empty or not.

If the specified role name does not exist, is null, or is an empty string, we recommend that your provider throw an exception.

Applies to

See also