问题
This may seem an odd question.
I have never used classes other than within simple tutorials etc.
What I am trying to do is to take text data that is in a datatable and assign certain cells or groups of cells to be a data class.
For example, say I had plumbing data I would have an overall class of part type that had things like part number, location, unit cost, manufacturer's name etc. as a top level class called, say, 'PART' and then a sub-class for specific parameters such as radius which only applies to bends or length which only applies to tubing.
The idea being that a number of cells are selected and then they are marked as containing data for an attribute within a class.
Firstly, am I on the wrong lines and secondly, if this approach is right how would I designate a cell or cells to be of a given class?
Edit: Datatable is created from strings in an opened user file containing unformatted data. It is then displayed in a datagrid.
回答1:
The first important thing is to know which way and what technology you want to use, which is the better for your context.
If the only "requirement" is to read some text from database and put that values to an intermediate data object, the consideration to take is:
- if the only data access is about reading just that values, it's not suitable to take an "over engineering" solution, for example ORMs are not the best solutions
- if it's important to take an elegant way to do, maybe some tiny orms like helpers maybe are good
The considerations could be more, now I will enumerate some tools:
- PetaPoco "A tiny ORM-ish thing for your POCOs" there are fine examples and instructions
- Massive "Massive is a Single File Database" there are very good examples and it's the fastest way to accomplish what you want to do in an elegant way
- SqlDataReaderToObject another faster way to accomplish what you want to do
- Extension methods for IDbComannd and IDataReader the fastest and costless solutions
Based on my experience I strongly suggest you a solution based on Massive, no assembly to install, just a code file to include in you solution, but, the fastest way, using datacomnnad and datareader from the point 4, could be a good solution.
Hope this helps.
回答2:
I suggest you to use linq to SQL. It is a part of ADO.NET
msdn.microsoft.com for linq to SQL
LINQ to SQL: .NET Language-Integrated Query for Relational Data
Simple LINQ to SQL in C#
来源:https://stackoverflow.com/questions/18013913/assigning-datatable-cells-to-a-class