Distinct Raw Data Population in DataTable





5.00/5 (1 vote)
How to do distinct raw data population in a DataTable
Occasionally during development (eg., when doing DataGrid binding and DataGrid paging when the Data Access Layer is incomplete) I had to populate a DataTable
instance with some raw data. I usally handle this specific scanario by writing custome code to add data in each DataTable
. But this was a boring process if I wanted to add let's say 100 distinct rows to my DataTable
. I decided to write the a basic Extension to get this job done. And here it is:
public static class RandomDataExtensions
{
static Random rand = new Random((int)DateTime.Now.Ticks);
public static void PopulateRawData(this DataTable dt, int maxRows)
{
if (dt != null )
{
DataRow dr;
for (long n = 0; n < maxRows; n++)
{
dr = dt.NewRow();
int i = 0;
foreach (DataColumn dc in dt.Columns)
{
dr[i++] = GetRandomData(dc.DataType.Name.ToLower());
}
dt.Rows.Add(dr);
}
}
}
/// <summary>
/// Get random data of specified type.Return DBNull.Value if type is not implimented.
/// Note that you can change the range the range of randomly generated data.
/// I have treated multiple types(i.e int and long) in single if statement as every int can be a long.
/// You can also rewrite if statement for each differenr type
/// if u really want to get explicit data for each type.
/// </summary>
public static object GetRandomData(string typeName)
{
object randData = DBNull.Value;//Set default value
if (typeName.Contains("int16") || typeName.Contains("short"))//short or ushort
randData = rand.Next(0,short.MaxValue);//range for accomodating both short and ushort
else if (typeName.Contains("int") || typeName.Contains("long"))
//int 32,int64,int,uint,long,ulong
randData = rand.Next(0,int.MaxValue);
else if (typeName.Equals("single") || typeName.Equals("float") ||
typeName.Equals("double"))
randData = (float)(rand.NextDouble() * rand.Next(0, 100));//Add random integer (0 - 100)
else if (typeName.Contains("string"))
randData = GetRandomString(5, 10);//Get random lower case string of length 5-10
else if (typeName.Contains("char"))
randData = (char)rand.Next(97, 122);//Get random lower case letter
else if (typeName.Contains("datetime"))
randData = new DateTime(rand.Next(1950, 2050), rand.Next(1, 12), rand.Next(1, 28));
//Get random date.You can change the range as you like
else if (typeName.Contains("bool"))
randData = rand.Next(0, 2) == 1 ? true : false;
return randData;
}
public static string GetRandomString(int minLen, int maxLen)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < rand.Next(minLen, maxLen); i++)
{
//97-122 lower case letter Ascii range.
//Range can be changed to include other characters
ch = Convert.ToChar(rand.Next(97, 122));
builder.Append(ch);
}
return builder.ToString();
}
}
And here is the calling code:
static void DataSetPopulate()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("DOB", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Type", typeof(char)));
dt.Columns.Add(new DataColumn("Points", typeof(float)));
dt.Columns.Add(new DataColumn("Married", typeof(Boolean)));
dt.Columns.Add(new DataColumn("Image", typeof(Byte[])));
//not implimented Random Data generation for this type
dt.Columns.Add(new DataColumn("ushort_test", typeof(ushort)));
dt.Columns.Add(new DataColumn("short_test", typeof(short)));
dt.Columns.Add(new DataColumn("uint_test", typeof(uint)));
dt.Columns.Add(new DataColumn("long_test", typeof(long)));
dt.Columns.Add(new DataColumn("ulong_test", typeof(ulong)));
dt.Columns.Add(new DataColumn("double_test", typeof(double)));
dt.PopulateRawData(10000);
}
There may be bugs in the code as I haven't thoroughly tested the code, so please comment if you find issues in the code. As far as performance is concerned, it is not so bad. The DataSetPopulate()
method runs in about 0.20 sec.