How to create and manage several datasets
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开发者_如何学C
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!
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.
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.
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
Are you sure you do not need one DataSet with multiple tables?
You can then name each table.
精彩评论