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

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

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

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

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

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

Visual Basic(声明)
Public MustOverride Function RoleExists ( _
    roleName As String _
) As Boolean
Visual Basic(用法)
Dim instance As RoleProvider
Dim roleName As String
Dim returnValue As Boolean

returnValue = instance.RoleExists(roleName)
C#
public abstract bool RoleExists (
    string roleName
)
C++
public:
virtual bool RoleExists (
    String^ roleName
) abstract
J#
public abstract boolean RoleExists (
    String roleName
)
JScript
public abstract function RoleExists (
    roleName : String
) : boolean

参数

roleName

要在数据源中搜索的角色的名称。

返回值

如果角色名已存在于已配置的 applicationName 的数据源中,则为 true;否则为 false

RoleExistsRoles 类的 RoleExists 方法调用,以确定一个角色名是否存在于已配置的 ApplicationName 的数据源中。

如果指定的 roleName 为 空引用(在 Visual Basic 中为 Nothing) 或是空字符串,则建议提供程序引发异常。

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

Visual Basic
Public Overrides Function RoleExists(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 [" & rolesTable & "]" & _
                                           " 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
C#
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 [" + rolesTable + "]"  +
                                    " 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;
}

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