Imports System.Text
Imports System.Random
Imports System.data
'/// <summary>
'/// This class can be used as a "helper" class to generate datatables with dummy
'/// values in order to stress-test or dummy-test data-driven applications without
'/// having to implement the Data Access Layer first.
'/// You can also use it to just generate strings filled with dummy values.
'/// </summary>
Public Class DataRetriever
Private m_objRnd As Random
'/// <summary>
'/// Default constructor.
'/// </summary>
Public Sub New()
'Create something as random as we can.
m_objRnd = New Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
End Sub
'/// <summary>
'/// Generates a datatable with a specified number of rows and columns and fills it
'/// with dummy values.
'/// </summary>
'/// <param name="rows">The number of rows the datatable will contain</param>
'/// <param name="cols">The number of columns the datatable will contain</param>
'/// <param name="minStringLength">Minimum random string length for column values</param>
'/// <param name="maxStringLength">Maximum random string length for column values</param>
'/// <returns>A DataTable object</returns>
Public Function GenerateDummyDataTable( _
ByVal rows As Int32 _
, ByVal cols As Int32 _
, Optional ByVal minStringLength As Int32 = 3 _
, Optional ByVal maxStringLength As Int32 = 15 _
) As DataTable
Dim objDt As New DataTable
'Add columns. Columns will have titles like "Col1", "Col2" etc.
For intCnt As Int32 = 1 To cols
objDt.Columns.Add("Col" + intcnt.ToString)
Next
'Add rows.
For intCnt As Int32 = 1 To rows
objDt.Rows.Add(GenerateDummyDataRowValues(cols, minStringLength, maxStringLength))
Next
Return objDt
End Function
'/// <summary>
'/// Generates a string filled with dummy characters with a random length
'/// within a specified range.
'/// </summary>
'/// <param name="minLength">The minimum length the string should have</param>
'/// <param name="maxlength">The maximum length the string should have</param>
'/// <returns>String</returns>
Public Function GenerateDummyValue( _
ByVal minLength As Int32 _
, ByVal maxlength As Int32 _
) As String
Dim intLength As Int32
intLength = m_objRnd.Next(minLength, maxlength)
Dim objSB As New StringBuilder(String.Empty)
'Create characters. Include Greek characters too. Change the range if you don't like that.
For intCnt As Int32 = 0 To intLength
objSB.Append(Chr(m_objRnd.Next(65, 228)))
Next
Return objSB.ToString
End Function
'/// <summary>
'/// Generates dummy object arrays to be used as values for DataRows
'/// </summary>
'/// <param name="cols">Number of columns</param>
'/// <param name="minLength">Minimum random string length for column values</param>
'/// <param name="maxLength">Maximum random string length for column values</param>
'/// <returns>Object()</returns>
Private Function GenerateDummyDataRowValues( _
ByVal cols As Int32 _
, Optional ByVal minLength As Int32 = 3 _
, Optional ByVal maxLength As Int32 = 15 _
) As Object()
Dim ob(cols - 1) As Object
For intCnt As Int32 = 0 To cols - 1
ob(intCnt) = New Object
ob(intCnt) = GenerateDummyValue(minLength, maxLength)
Next
Return (ob)
End Function
End Class