Sunday, June 16, 2013

How to Prepare a Demo Database


How many times you have the necessity of making a demo. Often this means you need to update database values.

You have to show real data, but at same time protect sensible customer information. For these reason I have thought a simple application that populate your database tables with “random” data or simply update or overwrite the existing values.

This Example operates with a demo database downloadable from this link. The database is SQLServer but you can easily move the example with other different databases.

For this application I connect both SQLServer and MySQL, using IDbConnection interface. You can download MySQL Connector from this web page (http://dev.mysql.com/doc/index-connectors.html).

The source code has many usefull class, like a RandomClass, but you can find of your interest the possibility to choose the values from different sources like file of names, surnames, companies etc.
I have made a method to move all date forward in order to have the last date in the table equal to the current date. If you have an historical database with old dates, you can arrange these in order to update dates toward today.

Of course you can have fun creating n variants from this example to your needs.



public class RandomClass
{
    Random random = new Random();

    // Generate a random integer between to values
    public int RandomInteger(int min, int max)
    {
        //Random random = new Random();
        return random.Next(min, max);
    }

    // Generate a random double number
    public double RandomFloat()
    {
        double mantissa = (random.NextDouble() * 2.0) - 1.0;
        double exponent = Math.Pow(2.0, random.Next(-126, 128));
        return (float)(mantissa * exponent);
    }

    // Generate a random double number
    public double RandomFloat(double min, double max)
    {
        return min + random.NextDouble() * (max - min);
    }

    // Generates a random string with the given length
    public string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
            return builder.ToString().ToLower();
        return builder.ToString();
    }

    // Generate a Random dateTime between Periods
    public DateTime RandomDateTime(DateTime from, DateTime to)
    {
        var range = to - from;
        var randTimeSpan = new TimeSpan((long)(random.NextDouble() * range.Ticks));
        return from + randTimeSpan;
    }

    // Generate a Random dateTime
    public DateTime RandomDateTime()
    {
        string dateString = "01/01/2000 08:30:00";
        DateTime from = DateTime.Parse(dateString);
        DateTime to = DateTime.Now;
        var range = to - from;
        var randTimeSpan = new TimeSpan((long)(random.NextDouble() * range.Ticks));
        return from + randTimeSpan;
    }
}