注意:此方法在 .NET Framework 2.0 版中是新增的。
获取一个值,该值指示指定角色名是否已存在于已配置的
applicationName 的角色数据源中。
命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)
Public MustOverride Function RoleExists ( _
roleName As String _
) As Boolean
Dim instance As RoleProvider
Dim roleName As String
Dim returnValue As Boolean
returnValue = instance.RoleExists(roleName)
public abstract bool RoleExists (
string roleName
)
public:
virtual bool RoleExists (
String^ roleName
) abstract
public abstract boolean RoleExists (
String roleName
)
public abstract function RoleExists (
roleName : String
) : boolean
参数
- roleName
要在数据源中搜索的角色的名称。
返回值
如果角色名已存在于已配置的 applicationName 的数据源中,则为 true;否则为 false。
RoleExists 由 Roles 类的 RoleExists 方法调用,以确定一个角色名是否存在于已配置的 ApplicationName 的数据源中。
如果指定的 roleName 为 空引用(在 Visual Basic 中为 Nothing) 或是空字符串,则建议提供程序引发异常。
下面的代码示例演示 RoleExists 方法的示例实现。
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
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