RoleProvider.CreateRole(String) 方法

定义

针对已配置的 applicationName,将一个新角色添加到数据源。

public:
 abstract void CreateRole(System::String ^ roleName);
public abstract void CreateRole (string roleName);
abstract member CreateRole : string -> unit
Public MustOverride Sub CreateRole (roleName As String)

参数

roleName
String

要创建的角色的名称。

示例

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

public override void CreateRole(string rolename)
{ 
  if (rolename == null || rolename == "")
    throw new ProviderException("Role name cannot be empty or null.");
  if (rolename.Contains(","))
    throw new ArgumentException("Role names cannot contain commas.");
  if (RoleExists(rolename))
    throw new ProviderException("Role name already exists.");
  if (rolename.Length > 255)
    throw new ProviderException("Role name cannot exceed 255 characters.");

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("INSERT INTO Roles "  +
                                    " (Rolename, ApplicationName) " +
                                    " Values(?, ?)", conn);

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

  try
  {
    conn.Open();

    cmd.ExecuteNonQuery();
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    conn.Close();      
  }
}
Public Overrides Sub CreateRole(rolename As String) 
  If rolename Is Nothing OrElse rolename = "" Then _
    Throw New ProviderException("Role name cannot be empty or null.")
  If rolename.Contains(",") Then _
    Throw New ArgumentException("Role names cannot contain commas.")
  If RoleExists(rolename) Then _
    Throw New ProviderException("Role name already exists.")
  If rolename.Length > 255 Then _
    Throw New ProviderException("Role name cannot exceed 255 characters.")

  Dim conn As OdbcConnection = New OdbcConnection(connectionString)
            Dim cmd As OdbcCommand = New OdbcCommand("INSERT INTO Roles " & _
                                                     " (Rolename, ApplicationName) " & _
                                                     " Values(?, ?)", conn)

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

            Try
                conn.Open()

                cmd.ExecuteNonQuery()
            Catch e As OdbcException
                ' Handle exception.
            Finally
                conn.Close()
            End Try
        End Sub

注解

CreateRoleCreateRole 类的 Roles 方法调用,以将指定角色添加到配置的 ApplicationName的数据源中。

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

如果指定的角色名称包含逗号,建议提供程序引发异常。

如果数据源限制角色名称的长度(例如,通过数据库中表的固定长度列),建议在角色名称超过数据源允许的最大长度时引发异常。

适用于

另请参阅