Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Filling an Array

Posted on 2003-03-13
8
Medium Priority
?
621 Views
Last Modified: 2012-08-13
I have some values that I would like to add to a DataTable.
There are multiples rows of data that I would like to fill a dataset using the loaddatarow method. this method takes an array object as input. the standard declaration for an array is

Dim myArray() as object={1,"Hello",2,0,5)
Dim dt as datatable

dt.loaddatarow(myArray,false)

that works just fine. My question is I have about 20 rows to add to the datatable. What is the shortest way to add them to the array and then to the dataset?

The fastest way I could figure was continue to declare more array variables
Dim myArray1
Dim myArray2..ets

but there has got to be a better way to bulk load the data into the array.

your comments are appreciated

0
Comment
Question by:rvindust
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8133425
When adding a large number of rows you can optimize the performance by using the LoadDataRow
method (which takes an array of the values to be assigned) and bracketing your code in DataTable's
BeginLoadData and EndLoadData methods.
These methods temporarily disable and then reenable  notifications, index maintenance and constraints
while loading data)

Just imagine that you have your data in the text delimited file (myData.dat) structured as follows

1;"Hello";2;0;5


Here is how to do it efficient

        ' Open the file, read its contents.
        Dim sr As New System.IO.StreamReader("myData.dat")
        Dim fileText As String = sr.ReadToEnd
        sr.Close()


        ' This regular expression defines a row of elements, and assigns a name
        ' to each group (that is, a field in the text row).
        Dim re As New System.Text.RegularExpressions.Regex("""(?<number1>[\d]+)"";""(?<mystring>[^""]+)"";(?<number2>[\d;]+);""(?<number3>[\d]+)"";""(?<number4>[\d]+)""")
        Dim ma As System.Text.RegularExpressions.Match

'turn off index maintenance and constraints
        dt.BeginLoadData()
        For Each ma In re.Matches(fileText)
            ' A new line has been found, so add a row to the table.
            ' Create an array of values and add it in one operation
            Dim myArray() As Object = {ma.Groups("number1").Value, ma.Groups("mystring").Value, _
                ma.Groups("number2").Value, ma.Groups("number3").Value, ma.Groups("number4").Value}
            dt.LoadDataRow(myArray, True)
        Next
'turn on index maintenance and constraints
        dt.EndLoadData()

====================================
i might make a mistake with regular expressions, but you've got the idea
0
 

Author Comment

by:rvindust
ID: 8138414
This is not a file read but hard coded values.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8138523
Yoy can hardcode these values to your array. Approach is the same
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 

Author Comment

by:rvindust
ID: 8147553
That is what I am asking. what is the best way to hard code them into the array.
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 200 total points
ID: 8147972
VB.NET support array which element of each is an array - known as jagged arrays

   ' this procedure tests arrays of arrays

    Sub TestJaggedArray()
        ' Initialize an array of arrays.
        Dim arr()() As String = {New String() {"a00"}, _
            New String() {"a10", "a11"}, _
            New String() {"a20", "a21", "a22"}, _
            New String() {"a30", "a31", "a32", "a33"}}

        ' Show how you can reference an element.
        Console.WriteLine(arr(3)(1))                    ' => a31

        ' Assign an entire row of elements.
        arr(0) = New String() {"a00", "a01", "a02"}

        ' Read an element just added.
        Console.WriteLine(arr(0)(2))                    ' => a02

        ' Expand one of the elements.
        ReDim Preserve arr(1)(3)
        ' Assign the new elements (now Nothing)
        arr(1)(2) = "a12"
        arr(1)(3) = "a13"
        ' Read back one of them
        Console.WriteLine(arr(1)(2))                    ' => a12
    End Sub
0
 

Author Comment

by:rvindust
ID: 8156294
So to reference row 0, array 0;
It would look like... arr(0)
0
 

Author Comment

by:rvindust
ID: 8156401
So to reference row 0, array 0;
It would look like... arr(0)
0
 

Author Comment

by:rvindust
ID: 8156424
Awesome. Thanks for your help. Just one change I had to make that is to declare the array as an object not a string.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question