使用英语阅读

通过


RoleProvider.GetAllRoles 方法

定义

获取已配置的 applicationName 的全部角色列表。

public abstract string[] GetAllRoles();

返回

String[]

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

示例

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

public override string[] GetAllRoles()
{
  string tmpRoleNames = "";

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("SELECT Rolename FROM Roles "  +
                                    " 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];
}

注解

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

如果配置的 applicationName不存在任何角色,我们建议提供程序返回一个没有元素的字符串数组。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅