使用英语阅读

通过


RoleProvider.RoleExists(String) 方法

定义

获取一个值,该值指示配置后的 applicationName 的角色数据源中是否已存在指定的角色名。

public abstract bool RoleExists(string roleName);

参数

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;
}

注解

RoleExistsRoleExists 类的 Roles 方法调用,以确定所配置的 ApplicationName的数据源中是否存在角色名称。

如果指定的角色名称为 null 或 为空字符串,建议提供程序引发异常。

适用于

产品 版本
.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

另请参阅