Fliggy Biography: Enumeration
Enumeration-are you a gentle trap? I recently read two enumeration articles in the garden: Careful enumeration trap and gentle enumeration trap. They all talk about one problem: enumeration binding on the front-end, the enumerated values are stored in the database. When the enumerated values are updated, the values in the database are not updated, which leads to a pile of inconsistent data.
In the system, we must all have encountered the use of enumeration to store data. For example, we need to display educational qualifications, but the educational qualifications are not often changed, so we should use enumeration, in the database, it is too troublesome to create a new table and link it to the left.
The following code is available:
View sourceprint? 1 Public Enum estuetype {elementary school, middle school, university ,}
Then the database stores 0, 1, and 2, which are used to represent primary, secondary, and university respectively.
But later, new requirements came, and the middle school needs to be changed to junior and senior. Some people assume that they directly change "Middle School" to "Junior High School", and then add "High School" between "Junior High School" and "university". The compilation is correct, OK, and they are mounted to the server.
Then the customer responded, okay? Didn't I enter a university? How has it become a high school student? The Administrator compiled vs for half a day and finally found that after adding a "high school", the value of "high school" changed to 2, that is to say, all the original academic qualifications in the database are 2 (previously expressed as college students), and now they all become high school students. Ah, it's all about this gentle enumeration, so I began to abandon enumeration.
There are two common methods to solve this problem:
1. Create a new table to store educational qualifications
2. Then use the gentle enumeration.
The first method solves this problem, but the problem is that a new table will be created in the database and there will be a table with few updates. Another problem is that when the data volume is large, the left join will affect the performance.
The second method requires us to solve the enumeration problem very well. Next I will talk about how to use enumeration.
We also have two ways to use enumeration: first, assign values to the enumeration:
Public Enum estuetype
{
Elementary school = 0,
Middle school = 1,
University = 2,
}
OK, you want to add a new high school, right? It's hard for me:
Public Enum estuetype
{
Elementary school = 0,
Junior high school = 1,
High school = 3,
University = 2,
}
New Problem: DBO query data: Select * from [Table name], and then find that the user's educational background here, Yi 0123 assumes that
0 indicates Primary School
1 stands for Junior High School
2 stands for High School
3 stands for the University
OK, the next operation is very dangerous: He wants to take a "pre-marital education training course" for all college students ...........
As a result, we thought, if we want to store the name directly in our database, instead of the value, that is, the field of education in the table is directly stored as "university ".
Well, this method is good and the problem is solved.
However, it seems that the first course of programming has been said: Do not use Chinese. There are two reasons:
1. Chinese occupies more database space
2. Chinese programming makes people feel less professional.
So I thought: how to achieve this: the database only stores the English name, such as the University. People will know about the university at first glance, but on the front desk, I want to display the two Chinese characters "". The Flying Pigeon text is as follows:
So I wrote a general enumeration class:
Enumeration operation class
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. reflection; using system. data; using system. windows. forms; namespace windowsformsapplication1 {public class enumutils <tenum> {public static datatable getallenums () {type T = typeof (tenum); fieldinfo [] fieldinfolist = T. getfields (); datatable dt = new datatable (); DT. columns. add ("text", typeof (string); DT. Columns. Add ("value", typeof (string); foreach (fieldinfo tfield in fieldinfolist) {If (! Tfield. isspecialname) {datarow DR = DT. newrow (); Dr ["value"] = tfield. name; enumdescriptionattribute [] enumattributelist = (enumdescriptionattribute []) tfield. getcustomattributes (typeof (enumdescriptionattribute), false); If (enumattributelist! = NULL & enumattributelist. length> 0) {Dr ["text"] = enumattributelist [0]. description;} else {Dr ["text"] = tfield. name;} DT. rows. add (DR) ;}return DT ;}public static string gettext (tenum enuminstance) {type T = typeof (tenum); fieldinfo [] fieldinfolist = T. getfields (); string strreturn = string. empty; foreach (fieldinfo tfield in fieldinfolist) {If (! Tfield. isspecialname & tfield. name. tolower () = enuminstance. tostring (). tolower () {enumdescriptionattribute [] enumattributelist = (enumdescriptionattribute []) tfield. getcustomattributes (typeof (enumdescriptionattribute), false); If (enumattributelist! = NULL & enumattributelist. length> 0) {strreturn = enumattributelist [0]. description; break ;}}return strreturn;} public static string getvalue (tenum enuminstance) {type T = typeof (tenum); fieldinfo [] fieldinfolist = T. getfields (); string strreturn = string. empty; foreach (fieldinfo tfield in fieldinfolist) {If (! Tfield. isspecialname & tfield. name. tolower () = enuminstance. tostring (). tolower () {strreturn = tfield. name; break ;}}return strreturn;} public static tenum getenum (string value) {type T = typeof (tenum); fieldinfo field = T. getfield (value); Return (tenum) system. enum. parse (T, value);} public static void bindlistcontrol (listcontrol) {listcontrol. displaymember = "text"; listcontrol. valuemember = "value"; listcontrol. datasource = getallenums ();} [attributeusage (attributetargets. enum | attributetargets. field, allowmultiple = false)] public class enumdescriptionattribute: attribute {private string description; Public String description {get {return this. description ;}} public enumdescriptionattribute (string description): Base () {This. description = Description ;}}}
The following is an enumeration statement of the Flying Pigeon family:
Code
Public Enum estuetype {[enumdescription ("")] primaryschool, [enumdescription ("")] juniorhighschool, [enumdescription ("")] highschool, [enumdescription ("")] University}
Flying Pigeon book: http://www.freeeim.com/
Front-end binding: enumutils <estuetype>. bindlistcontrol (this. combox1 );
When you obtain the bound value (Enumeration type): enumutils <estuetype>. getenum (this. combobox1.selectedvalue. tostring ());;
When the display value is obtained (enumeration display name): enumutils <estuetype>. gettext (estuetype. highschool );
The following is an example:
OK. All problems are solved.