简体   繁体   中英

VB.NET selecting multiple columns of MySql table simultaneously into lists

I have a MySQL table that looks like this (attached below).

I want to simultaneously select (in a single SQL Query) multiple columns ( id , Last Name , and username ) and import into them into different lists of strings the values if they meet a certain condition (in this case, where color="blue" .

+----+------------+-----------+----------+----------------+
| id | First Name | Last Name | Username | Favorite Color |
+----+------------+-----------+----------+----------------+
|  1 | John       | Smith     | jsmith   | Blue           |
|  2 | Avery      | Nelson    | aNelson  | Red            |
|  3 | Jack       | Brooklyn  | jBrook   | Blue           |
|  4 | Arnold     | Nam       | aNam     | Blue           |
|  5 | Charlie    | Smith     | cSmith   | Orange         |
+----+------------+-----------+----------+----------------+
... Continued

I am trying to select all the required data that meet the condition where color=Blue with the MySQL query of SELECT id, Last Name, username FROM `myTable` WHERE color="Blue" . Once this data is selected, I want to import each selected column that meets the color requirement into separate lists.

For example, list FirstName should be list of strings "John, Jack, Arnold" (in that order) and Username list should contain "jsmith, jBrook, aNam" etc. In the end, I want to be able to produce three lists that contain these values which meet the Favorite Color condition in the MySQL database of Blue . How can I do this?

I know that I can make three separate reader queries but I want to do them all in one to save time.

You have not posted VB code so I'm not sure how you intend to use the results but you could load the whole table to a Datatable (unless it is really big), then use Dataview.RowFilter to filter your data with different criteria in-memory , without making any further request to the database backend.

You can see an example in VB.net here

Then you could use LINQ for example to generate a list of string from a datatable or view, see here for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM