RoleProvider.RoleExists(String) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示配置后的 applicationName 的角色数据源中是否已存在指定的角色名。
public:
 abstract bool RoleExists(System::String ^ roleName);public abstract bool RoleExists(string roleName);abstract member RoleExists : string -> boolPublic MustOverride Function RoleExists (roleName As String) As Boolean参数
- roleName
- String
要在数据源中搜索的角色名。
返回
如果配置后的 applicationName 的数据源中已存在角色名,则为 true;否则为 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 由 RoleExists 类的 Roles 方法调用,以确定所配置的 ApplicationName的数据源中是否存在角色名称。
如果指定的角色名称为 null 或 为空字符串,建议提供程序引发异常。