共用方式為


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或是空字串,建議您的提供者擲回例外狀況。

如果指定的角色名稱包含逗號,建議您的提供者擲回例外狀況。

如果您的數據源限制角色名稱的長度,例如,透過資料庫中數據表的固定長度數據行,建議您在角色名稱超過數據源所允許的最大長度時擲回例外狀況。

適用於

另請參閱