简体   繁体   中英

Loading a lookup table from a database into a C# program - data structure?

I have a table full of id's,categories and weights that I need to reference in my program as I read in records that contain those categories. What is the most efficient method to read those from a database and put into a structure that I can reference?

The ID's (and possibly the names) would be unique

Data might look like:

ID,Category,Weight
1,Assignment,5
2,Test,10
3,Quiz,5
4,Review,3

Your best bet is to read in your table using a DataReader, and put each row into an object containing Category and Weight, then each object into a Dictionary.

If you're using a later version of .NET, you could always use Linq to just grab that data for you.

If you want to avoid a database hit to fetch static data, you can hard-code the values into a common class in your solution. A Dictionary collection would work fine here too.

The trade off of course is; 2 locations to manage for any possible future changes.

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