다음을 통해 공유


RoleProvider.RoleExists(String) 메서드

정의

지정된 역할 이름이 구성된 applicationName의 역할 데이터 원본에 이미 있는지 여부를 나타내는 값을 가져옵니다.

public:
 abstract bool RoleExists(System::String ^ roleName);
public abstract bool RoleExists (string roleName);
abstract member RoleExists : string -> bool
Public MustOverride Function RoleExists (roleName As String) As Boolean

매개 변수

roleName
String

데이터 원본에서 검색할 역할의 이름입니다.

반환

Boolean

역할 이름이 구성된true 의 데이터 원본에 이미 있으면 applicationName이고, 그러지 않으면 false입니다.

예제

다음 코드 예제에서는 메서드의 샘플 구현을 RoleExists 보여줍니다.

public override bool RoleExists(string rolename)
{
  if (rolename == null || rolename == "")
    throw new ProviderException("Role name cannot be empty or null.");

  bool exists = false;

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("SELECT COUNT(*) 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;

  try
  {
    conn.Open();

    int numRecs = (int)cmd.ExecuteScalar();

    if (numRecs > 0)
    {
      exists = true;
    }
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    conn.Close();      
  }

  return exists;
}
Public Overrides Function RoleExists(ByVal rolename As String) As Boolean

    If rolename Is Nothing OrElse rolename = "" Then _
      Throw New ProviderException("Role name cannot be empty or null.")

    Dim exists As Boolean = False

    Dim conn As OdbcConnection = New OdbcConnection(connectionString)
    Dim cmd As OdbcCommand = New OdbcCommand("SELECT COUNT(*) 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

    Try
        conn.Open()

        Dim numRecs As Integer = CType(cmd.ExecuteScalar(), Integer)

        If numRecs > 0 Then
            exists = True
        End If
    Catch e As OdbcException
        ' Handle exception.
    Finally
        conn.Close()
    End Try

    Return exists
End Function

설명

RoleExists는 클래스의 Roles 메서드에 의해 RoleExists 호출되어 구성된 ApplicationName데이터 원본에 역할 이름이 있는지 여부를 확인합니다.

지정된 역할 이름이 null 빈 문자열이거나 빈 문자열인 경우 공급자가 예외를 throw하는 것이 좋습니다.

적용 대상

추가 정보