Note that this program assumes that the table contains an Index field that numbers its records starting with 0.
When you click the Go button, the program calls function GetDbConnection to open a database connection. The program then executes a query to see how many records are in the table.
Next the program makes an array to hold the indexes that it selects. It makes a SortedList to keep track of the selected indexes in numeric order.
It then enters a loop to select the indexes.
For each index, the program picks a random number between 0 and the number of records that have not yet been selected. For example, if the table contains 10 records then it picks between 0 and 9, 0 and 8, 0 and 7, and so forth.
The code then loops through the previously selected indexes from smallest to biggest. Each time it finds an index <= the newly selected index, it increments the newly selected index.
For example, suppose the program has already selected indexes 2, 4, and 6, and now selects index 3. To ensure that it selects from all of the unpicked indexes, it must increment the new index for 2 (new value = 4), and 4 (new value = 5).
Now the program loops through the selected records, selecting each from the table by using its index and displaying it in the ListBox. (If someone knows how to select the i-th record from the table without using an index field and without looping through the entire table, let me know.)
|