SQL Data Generator - 2.0
Creating new generators - SQL Data Generator
You may want to create a new generator if the supplied generators do not meet your requirements and you cannot customize them to suit your needs.
To write your own generator, you must be proficient at a .NET 2.0 language, have a good understanding of .NET, and have access to SQL Data Generator on your computer.
The procedure is summarized below:
- In Microsoft Visual Studio, create a Class Library .NET project.
- Add references to RedGate.SQLDataGenerator.Engine and RedGate.SQLCompare.Engine
- Create a public class that implements IGenerator.
- Add the class attributes.
- Implement the constructor.
- Implement the method GetEnumerator.
- Copy the output DLL to:
Program Files\Red Gate\SQL Data Generator 1\Generators
Example Microsoft® Visual Studio® 2005 project files are provided in:
The examples are written in C#. If you want to build the projects, you must first add the project references for the SQL Compare Engine and the SQL Data Compare Engine. You may also want to change the output path. When you have built the project, copy the output DLL to:
Architecture
A simple diagram of the architecture is shown below.

The SQL Data Generator engine defines a series of interfaces. Each interface is very lightweight.
A generator must implement a series of interfaces in order that the engine considers it to be a generator. At startup, a specified folder is scanned for DLLs. Each DLL is loaded in turn, and reflection is used to check whether any public classes implement these interfaces. If they do, the class is considered to be a generator and is made accessible to the rest of the system.
By default, parameters for the generator are displayed in a standard Microsoft Grid Control. To override this default functionality, implement interfaces IGeneratorUIStyle and IGeneratorUI. See the FullDemo project for an example.
Basic interface: IGenerator
The IGenerator interface must be implemented for your class to be considered a generator.
The IGenerator interface is defined as:
namespace RedGate.SQLDataGenerator.Engine.Generators
public interface IGenerator
{
IEnumerator GetEnumerator
GenerationSession session);
}
}
You must also implement a special constructor that takes a single parameter of type GeneratorParameters. This parameter describes the SQL field in the Table that is being assigned. If necessary, your code can throw exceptions and so on.
To display your generator in the graphical user interface (GUI), you must add a simple Generator attribute to your class. The following example code produces random values between 0 and 1024 for the 8 times table.
namespace Basic
{
[Generator(typeof(int), "Generic", "8 times table",
"8, 16, 0, 256, ...")]
public class Basic : IGenerator
{
public Basic(GeneratorParameters parameters)
{
}
public System.Collections.IEnumerator GetEnumerator
(GenerationSession session)
{
Random r = new Random(0);
while (true)
{
yield return r.Next(0, 1024) * 8;
}
}
}
}
The Generator attribute defines the type of .NET result, the Category that the generator is to be placed in, and the name and description to be displayed in the GUI. It must be defined only once per class.
SQL Data Generator assigns the SQL data type that corresponds to the specified type of .NET result. To create a generator that supports multiple SQL data types, add SupportSQLType. SQL Data Generator will add SQL data types based on SqlTypes defined in the SQL Compare engine.
Note that you must ensure that the class is public, and the Generator class exists.
Constructor
GeneratorParameters provides access to the field. This enables your code to verify that lengths and types are consistent.
GetEnumerator
The easiest way to implement this code is by using the Yield statement; the above example never runs out of values. However, it is not always possible to do this. The engine is designed to cater for a limited number of values from the GetEnumerator. If necessary, the GetEnumerator can throw exceptions.
Interface: ISeedableGenerator
This interface enables the generator to specify a seed. The generator can then generate random data that is different each time.
The ISeedableGenerator is defined as:
public interface ISeedableGenerator
{
int Seed { get; set; }
}
Use the seed to initialize the Random class in the GetEnumerator.
The engine will automatically give a value to Seed at initialization. Each column will have its own seed, therefore the same generator can be assigned multiple times within a table and different values will be produced for each column.
A typical implementation is:
public int Seed
{
get { return m_Seed; }
set { m_Seed = value; }
}
For a complete example, see:
Interface: IUniqueableGenerator
This interface enables the generator to specify whether the data generated is unique so that the generator can then generate a unique value.
The IUniqueableGenerator is defined as follows:
public interface IUniqueableGenerator
{
bool Unique { get; set; }
}
A typical implementation is:
public bool Unique
{
get { return m_Unique; }
set { m_Unique = value; }
}
For a complete example, see:
The generator can now be assigned to unique fields. The engine automatically configures the Unique flag as on when the generator is assigned.
See also |
Was this article helpful?
SQL Data Generator
- Creating random GUIDs in SQL Data Generator
- Conforming to unique constraints in SQL Data Generator
- Generating data in improperly-named column causes System.InvalidOperationException
- Self-referencing table constraints can cause generation to stop
- Logging and log files
all SQL products
- Compatibility of Red Gate tools in 64-bit environments
- Application has encountered an error and needs to close
- Error message after installing SQL Toolbelt - The description for Event ID ( 1 ) in Source ( nview_info ) cannot be found.
- Changing the temporary directory used by the installer
- Toolbelt Installer "hanging" while "scanning volumes"
- Login failing with "trusted SQL Server connection" error when using RunAs
all products
- Some Red Gate products identified as containing a trojan by Anti-Virus software
- Activation may fail with Unknown Error -1
- Product uses web help although a CHM file is available locally
- Argument exception resulting from missing environment variable
- Check for updates may fail when used through proxies
- 'Unidentified Publisher' error when repairing or uninstalling
- Licensing activates product as standard edition
- Moving Red Gate software products to another machine
- Red Gate tools log locations
- The application UI opening slowly when there is no internet access
SQL Data Generator
- Activating your products
- Activating your products
- Getting help offline
- Cleansing or removing sensitive data from an existing data source
- SQL Data Generator release notes - version 1.xx
- SQL Data Generator release notes - version 2.xx
all SQL products
all products
- Red Gate product acknowledgements
- Activating your products
- Activating your products
- Red Gate bundle history
- Check for updates
- Troubleshooting Check for Updates errors
- Current versions
- Deactivating your products
- Installing Red Gate products from the .msi file
- Requesting additional activations
- Serial numbers for bundles
- Reactivating using a different serial number
- Extending your trial
- Finding your serial numbers
- Moving a serial number from one computer to another
- No response received for manual activation
- Licensing and activation resources
- Licensing and activation resources
- Troubleshooting licensing and activation errors
- Licensing and activation FAQs
- Red Gate tools log file locations
- Download old versions of products
- Download product prerequisites & utilities
- Support & upgrades
- Upgrading your software
- Upgrading FAQs

Using SQL Data Generator