请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
RoleProvider 类
 GetAllRoles 方法

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
RoleProvider.GetAllRoles 方法

注意:此方法在 .NET Framework 2.0 版中是新增的。

获取已配置的 applicationName 的所有角色的列表。

命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public MustOverride Function GetAllRoles As String()
Visual Basic(用法)
Dim instance As RoleProvider
Dim returnValue As String()

returnValue = instance.GetAllRoles
C#
public abstract string[] GetAllRoles ()
C++
public:
virtual array<String^>^ GetAllRoles () abstract
J#
public abstract String[] GetAllRoles ()
JScript
public abstract function GetAllRoles () : String[]

返回值

一个字符串数组,包含在数据源中存储的已配置的 applicationName 的所有角色的名称。

GetAllRolesRoles 类的 GetAllRoles 方法调用,以从数据源检索角色名的列表。仅检索指定的 ApplicationName 的角色。

如果对于已配置的 applicationName 不存在任何角色,则建议提供程序返回不包含任何元素的字符串数组。

下面的代码示例演示 GetAllRoles 方法的示例实现。

Visual Basic
Public Overrides Function GetAllRoles() As String() 
  Dim tmpRoleNames As String = ""

  Dim conn As OdbcConnection = New OdbcConnection(connectionString)
  Dim cmd As OdbcCommand = New OdbcCommand("SELECT Rolename FROM [" & rolesTable & "]" & _
                                           " WHERE ApplicationName = ?", conn)

  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName

  Dim reader As OdbcDataReader = Nothing

  Try
    conn.Open()

    reader = cmd.ExecuteReader()

    Do While reader.Read()
      tmpRoleNames &= reader.GetString(0) & ","
    Loop
  Catch e As OdbcException
    ' Handle exception.
  Finally
    If Not reader Is Nothing Then reader.Close() 
    conn.Close()      
  End Try

  If tmpRoleNames.Length > 0 Then
    ' Remove trailing comma.
    tmpRoleNames = tmpRoleNames.Substring(0, tmpRoleNames.Length - 1)
    Return tmpRoleNames.Split(CChar(","))
  End If

  Return New String() {}
End Function
C#
public override string[] GetAllRoles()
{
  string tmpRoleNames = "";

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("SELECT Rolename FROM [" + rolesTable + "]"  +
                                    " WHERE ApplicationName = ?", conn);

  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;

  OdbcDataReader reader = null;

  try
  {
    conn.Open();

    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
      tmpRoleNames += reader.GetString(0) + ",";
    }
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    if (reader != null) { reader.Close(); }
    conn.Close();      
  }

  if (tmpRoleNames.Length > 0)
  {
    // Remove trailing comma.
    tmpRoleNames = tmpRoleNames.Substring(0, tmpRoleNames.Length - 1);
    return tmpRoleNames.Split(',');
  }

  return new string[0];
}

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker