共用方式為


Generator 類別

表示資料產生器的抽象基底類別。

繼承階層架構

System.Object
  Microsoft.Data.Schema.Tools.DataGenerator.Generator

命名空間:  Microsoft.Data.Schema.Tools.DataGenerator
組件:  Microsoft.Data.Schema.Tools (在 Microsoft.Data.Schema.Tools.dll 中)

語法

'宣告
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator _
    Implements IGenerator, IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public ref class Generator abstract : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[<AbstractClass>]
[<GeneratorAttribute(typeof(DefaultGeneratorDesigner))>]
[<CLSCompliantAttribute(true)>]
type Generator =  
    class
        interface IGenerator
        interface IDisposable
        interface IExtensionInformation
        interface IExtension
    end
public abstract class Generator implements IGenerator, IDisposable, IExtensionInformation, IExtension

Generator 型別會公開下列成員。

建構函式

  名稱 說明
受保護的方法 Generator 從衍生類別的建構函式呼叫,以初始化 Generator 類別。

回頁首

屬性

  名稱 說明
公用屬性 Collation 取得或設定這個資料行的定序或語言的適當排序。
公用屬性 Column 取得或設定資料行。
公用屬性 ColumnName 取得或設定資料行名稱。
公用屬性 Constraints 取得或設定資料行的檢查條件約束資訊。
公用屬性 DefaultLocaleId 取得或設定地區設定 ID。
公用屬性 DefaultValue 取得或設定此資料行的預設值。
公用屬性 ExtensionHandle 取得擴充控制代碼。
公用屬性 MaxLength 取得或設定這個資料行中值的最大長度。
公用屬性 Nullable 取得或設定值,這個值表示此資料行是否可以設為 nullNull 參照 (即 Visual Basic 中的 Nothing)。
公用屬性 OutputKey 取得或設定用來擷取輸出值的索引鍵。
公用屬性 PercentageNull 取得或設定使用者希望此資料行之產生值為 nullNull 參照 (即 Visual Basic 中的 Nothing) 的百分比。此百分比會以 0 到 100 之間的整數來表示。
公用屬性 Precision 取得或設定資料的精確度。
公用屬性 RowsToInsert 取得或設定要產生的資料列數目。
公用屬性 Scale 取得或設定資料的比例。
公用屬性 SchemaName 取得或設定此資料行所屬的結構描述名稱。
公用屬性 Seed 取得或設定種子。您可以使用此種子來初始化具決定性的隨機資料產生演算法。
公用屬性 TableName 取得或設定此資料行所屬的資料表名稱。
公用屬性 TargetType 取得或設定資料行的目標資料型別。
公用屬性 Unique 取得或設定值,這個值表示所產生的資料行是否屬於唯一條件約束或主索引鍵條件約束的一部分。

回頁首

方法

  名稱 說明
公用方法 Dispose() 釋放 Generator 所使用的所有資源。
受保護的方法 Dispose(Boolean) 釋放 Generator 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。
公用方法 Equals 判斷指定的 Object 和目前的 Object 是否相等。 (繼承自 Object)。
受保護的方法 Finalize 允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。 (繼承自 Object)。
公用方法 GenerateNextValues 根據資料產生演算法產生下一個值。
公用方法 GetHashCode 做為特定型別的雜湊函式。 (繼承自 Object)。
公用方法 GetOutputValue 使用 OutputKey 來擷取輸出值。
公用方法 GetType 取得目前執行個體的 Type。 (繼承自 Object)。
公用方法 Initialize 初始化資料產生器。
受保護的方法 MemberwiseClone 建立目前 Object 的淺層複本 (Shallow Copy)。 (繼承自 Object)。
受保護的方法 OnGenerateNextValues 根據資料產生演算法產生下一個值。
受保護的方法 OnGetOutputValue 擷取目前包含一個或多個值的結果集。
受保護的方法 OnInitialize 初始化產生器。
受保護的方法 OnNextValues 產生資料列的下一組值。
受保護的方法 OnSetInputValues 使用提供的具名值清單,設定資料產生器的輸入屬性。
受保護的方法 OnValidateInputs 驗證資料產生器的所有必要輸入是否都已設定。
公用方法 SetExtensionHandle 使用指定的擴充控制代碼,設定擴充控制代碼。
公用方法 SetInputValues 設定資料產生器的輸入值。
公用方法 ToString 傳回表示目前物件的字串。 (繼承自 Object)。
公用方法 ValidateInputs 驗證資料產生器的所有必要輸入是否都已設定。

回頁首

備註

如果標準資料產生器不夠,您可以建立自訂的資料產生器。 您必須建立實作 IGenerator 或繼承自 Generator 的類別 (Class),才能建立自訂資料產生器。 您可以用 GeneratorAttribute 為類別進行裝飾,使其識別為資料產生器。

您可以為自訂資料產生器建立自訂的設計工具,或是您可以使用 DefaultGeneratorDesigner

基底類別實作會建構以 OutputAttribute 標記之公用屬性為基礎的輸出。 這些輸入是使用 InputAttribute 所設定。 使用以屬性 (Attribute) 標記的屬性 (Property) 會提供一項簡單的機制,可用於宣告強型別 (Strongly Typed) 的輸入和輸出值。

範例

標準資料產生器無法產生資料來滿足某些檢查條件約束 (Check Constraint)。 例如,您的檢查條件約束要求日期位於兩個相異範圍的其中一個內,您將無法使用標準 DateTime 產生器。 這個範例會建立一個可以滿足這類條件約束的自訂資料產生器。 產生器會接受兩個不同的範圍做為輸入,而且會產生位於其中一個範圍的隨機日期。 如需詳細資訊,請參閱逐步解說:為檢查條件約束建立自訂資料產生器

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;

namespace GeneratorDateRangesCS
{
    public class GeneratorDateRangesCS : Generator
    {
        DateTime mRange1Min;
        DateTime mRange1Max;

        DateTime mRange2Min;
        DateTime mRange2Max;

        [Input]
        public DateTime Range1Min
        {
            set {mRange1Min = value;}
        }

        [Input]
        public DateTime Range1Max
        {
            set {mRange1Max = value;}
        }

        [Input]
        public DateTime Range2Min
        {
            set {mRange2Min = value;}
        }

        [Input]
        public DateTime Range2Max
        {
            set {mRange2Max = value;}
        }

        DateTime mRandomDate;

        [Output]
        public DateTime RandomDate
        {
            get {return mRandomDate;}
        }

        Random mRandom;
        Random mRandomRange;

        protected override void OnInitialize(GeneratorInit initInfo)
        {
            // Deterministic
            mRandom = new Random(this.Seed);
            mRandomRange = new Random(this.Seed);  //deterministic

            // Non-deterministic
            //mRandom = new Random();
            //mRandomRange = new Random();

            base.OnInitialize(initInfo);
        }

        protected override void OnGenerateNextValues()
        {
            DateTime min;
            DateTime max;

            // Generate a random date from either range 1 or range 2.
            // Randomly select either range 1 or range 2 by randomly 
            // generating an odd or an even random number.
            if (mRandomRange.Next() % 2 == 0)  //check for odd or even
            {
                min = mRange1Min;
                max = mRange1Max;
            }
            else
            {
                min = mRange2Min;
                max = mRange2Max;
            }

            // The formula for creating a random number in a specific
            // range is: of range + (size of range * random number 
            // between 0 and 1)

            //size of range
            TimeSpan range = max - min;

            // (Size of range * random number between 0 and 1)
            TimeSpan randomNumber = new TimeSpan(
                (long)(range.Ticks * mRandom.NextDouble()));

            // Start of range + (size of range * random number 
            // between 0 and 1)
            mRandomDate = min + randomNumber;
        }
    } // End of class
}     // End of namespace
Imports Microsoft.VisualStudio.TeamSystem.Data.DataGenerator

Public Class GeneratorDateRangesVB
    Inherits Generator

    Dim mRange1Min As Date
    Dim mRange1Max As Date

    Dim mRange2Min As Date
    Dim mRange2Max As Date

    <Input()> _
    Public WriteOnly Property Range1Min() As Date
        Set(ByVal value As Date)
            mRange1Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range1Max() As Date
        Set(ByVal value As Date)
            mRange1Max = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Min() As Date
        Set(ByVal value As Date)
            mRange2Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Max() As Date
        Set(ByVal value As Date)
            mRange2Max = value
        End Set
    End Property

    Dim mRandomDate As Date

    <Output()> _
    Public ReadOnly Property RandomDate() As Date
        Get
            Return mRandomDate
        End Get
    End Property

    Dim mRandom As Random
    Dim mRandomRange As Random

    Protected Overrides Sub OnInitialize(ByVal initInfo _
        As GeneratorInit)

        mRandom = New Random(Me.Seed)       'deterministic
        mRandomRange = New Random(Me.Seed)  'deterministic

        'mRandom = New Random()              'non-deterministic
        'mRandomRange = New Random()         'non-deterministic

        MyBase.OnInitialize(initInfo)
    End Sub

    Protected Overrides Sub OnGenerateNextValues()
        Dim min As Date
        Dim max As Date

        ' Generate a random date from either range 1 or range 2.
        ' Randomly select either range 1 or range 2 by randomly 
        ' generating an odd or an even random number.
        '------------------------------------------------------------
        If mRandomRange.Next() Mod 2 = 0 Then  'check for odd or even
            min = mRange1Min
            max = mRange1Max
        Else
            min = mRange2Min
            max = mRange2Max
        End If

        ' The formula for creating a random number in a specific 
        ' range is: of range + (size of range * random number 
        ' between 0 and 1)

        ' Size of range
        Dim range As TimeSpan = max - min

        ' (Size of range * random number between 0 and 1)
        Dim randomNumber As TimeSpan = _
            New TimeSpan(CLng(range.Ticks * mRandom.NextDouble()))

        ' Start of range + (size of range * random number 
        ' between 0 and 1)
        mRandomDate = min + randomNumber
    End Sub
End Class

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

Microsoft.Data.Schema.Tools.DataGenerator 命名空間

GeneratorAttribute

GeneratorInit

IGenerator

其他資源

逐步解說:為檢查條件約束建立自訂資料產生器

Creating Custom Data Generators

資料產生器擴充性概觀

Specifying Details of Data Generation for a Column