views:

139

answers:

3

What is the best way for me to have several datasets ?

i've thought about creating a routine for creating datasets with a number in their names, each one with the desired data, but that's not very intelligent (although might work)

i'd like to create what would be like an "ARRAY OF DATASETS"
vb:

<whatever> = ds(1).<whatever>
<whatever> = ds(2).<whatever>
...
...

and beign able to see how many datasets i have in this "array" would be desirable.

any answer in c# or vb.net is welcome! (although i'm developing in vb.net)

i've tried doing it like an array

    Dim dinfo As DirectoryInfo = New DirectoryInfo("c:\")  
    Dim finfo As FileInfo() = dinfo.GetFiles("*.xml")
    Dim ds() As DataSet = Nothing

    For a = 0 To finfo.GetLength(0) Step 1

        ds(a).ReadXml("c:\" + finfo(a).Name)
    Next

and i get

***Object reference not set to an instance of an object.***

so i assume it just isn't like this.

(obs, i've done the same code loading one XML to one dataset and it works perfectly, i've gone back and forth more than one time to assure that it isn't just a logical mistake, it's syntax and lack of programming knowlegde.. haha)

Thank you!

+1  A: 

well you can certainly do it by simply:

Dim ds(100) as DataSet

That will work. However, a dataset can contain multiple tables, making an array of datasets a sort-of 4th dimentional array (dataset, table, row, column). If you really need this functionality, then you can create an array as stated above, or use an array list, or whichever other .net collection class best fits your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~Edit after seeing your edit~~~~~~~~~~~~~~~~~~~~~~

The reason you are getting null reference exception is that the array has zero objects in it if you declare it without a number. If you do not know how many datasets you will need at design time then you should probably consider using an arraylist or one of the .net collections. There is a statement you could run that would basically be: "Redim Preserve ds(ds.length)" that would increase the size of the array by 1 so you could fill the next one, but I don't recommend it. This recreates the entire array in memory, copies the values, and then deletes the old one.

~~~~~~~~~~~~~~~~~~~~~~~~~~ArrayList version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alright, here is how it would look with an ArrayList:

Dim dinfo As DirectoryInfo = New DirectoryInfo("c:\")
Dim finfo As FileInfo() = dinfo.GetFiles("*.xml")
Dim ds As New ArrayList()

For a As Integer = 0 To finfo.GetLength(0)
    Dim tempDS As New DataSet
    tempDS.ReadXml("c:\" + finfo(a).Name)
    ds.Add(tempDS)
Next

~~~~~~~~~~~~~~~~~~~~~~~~~~Array version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is how it would look with the regular array if you don't feel comfortable with the arrayList:

Dim dinfo As DirectoryInfo = New DirectoryInfo("c:\")
Dim finfo As FileInfo() = dinfo.GetFiles("*.xml")

Dim ds(-1) As DataSet
For a As Integer = 0 To finfo.GetLength(0)
    ReDim Preserve ds(ds.Length)
    ds(ds.Length - 1) = New DataSet()
    ds(ds.Length - 1).ReadXml("c:\" + finfo(a).Name)
Next
Jrud
i can't.. it has to be able to get as many xmls as it could.. it may be up to 6 thousand in some cases.
MarceloRamires
you didn't mention that you had an exception the first time :P I made an edit explaining what to do. If you would like I can go into more detail about the ArrayList collection.
Jrud
or the Redim statement if you REALLY, REALLY want to, but I'm saying, its bad programming practice.
Jrud
redim ? well i mean it when i say i'm new.. and why is it bad? memory consumption?
MarceloRamires
I just figured out a way.. what i want from the xml's are some few fields.. so i'm gonna load ONE, get the fields.. then load the next one OVER it. that way, is this the "good practice" you're mentioning? =D
MarceloRamires
yes, this will be much better practice. If you do that, you shouldn't ever be left with this huge chunk of memory consumed by the dataset array because you won't have one. I put up a demonstration of how to use the redim statement so you could see what it does. The reason its bad is because of memory consumption of its existance, and the fact that it DOUBLES the memory used while its copying to the new array, and then there is a lot of processor overhead to copy that much memory as well creating a LOT of overhead for your app.
Jrud
Since you're probably new to stack overflow, I would advise you to post an answer to your own question here, and the check it as the correct answer, then upvote anyone who assisted you.
Jrud
yes, i'm new. that's exactly what i did.. i figured it out just before you said it, haha! thanks a lot for helping me!
MarceloRamires
you are very welcome.
Jrud
+2  A: 

The answer to the past marcelo is DON'T DO IT.
datasets are large, and by using one you're already consuming too much memory.
i'd personally like to thank JRUD, who has explained it to me (alredy voted up), and i just couldn't choose his answer as right because he didn't lead me to success by his answer, but by the comments.

what i did: created ONE dataset and used it (got the fields i desired) then cleaned it:

dim ds as new dataset

this line of code was in the FOR where i loaded each file, so it flushes every time. worked perfectly.

MarceloRamires
perfect, now just accept your own answer when it lets you and the thread is complete.
Jrud
A: 

Are you sure you do not need one DataSet with multiple tables?

You can then name each table.

Carra