steve

steve

  • NA
  • 23
  • 0

Need help with how to read database data of apriori unknown size into a 2D array.

Jul 2 2008 1:42 PM

I have a task which is very simple to script in PHP, which I have worked mostly with for the past several years, but I can't figure out a good way to do it in C#. I hope someone can help.

I am reading x and y coordinates and a corresponding value from a database. This is a mapping of chips on a wafer, and is relatively round, meaning that the x,y pairs which are present will not fill completely a rectangular array. 

I do know that the x and y coordinates start at 1 (not zero), but until I have retrieved all the data from the database, I do not know the maximum value for x or for y. And I do not know the total number of rows returned, since the MySQL/Net connector I am using does not seem to be able to provide this.

What I wish to do is to have the data placed into either a rectangular array, or, for more memory efficiency, perhaps a jagged array, such that I can access the values with for example myarray[x,y] and so I can traverse the values using nested foreach loops.

But since I do not know the size of the mapping (total number of rows returned, maximum x, maximum y) I cannot define a dynamic array, or at least I do not know how to. Also, since neither x nor y cannot take on the value of 0, my arrays should be indexed to start at 1, not 0.

Can anyone tell me how this best could be done? I came up with a way today which to me does not seem too efficient: I retrieve the data and put them into a hashtable of hashtables, and calculate the maximum x and y while doing this. I then create a rectangular array of size [max x, max y] filled initially with zeros, and then fill this array with the values from the hashtables. This works, but it just does not seem very efficient to me.

Thanks for any help!


Answers (2)