A scheme for fast access to selection entries (asp.net+c#)

Source: Internet
Author: User
Tags count reset split
asp.net introduction:

We are in the process of doing project development, often encounter a lot of information on the selection of items recorded in the user's relevant settings of the problem, the more stupid way is that each entry corresponds to the user set a property field in the table, but such a design seems to be very poor, efficiency is not good where to go, This provides a quick way to store N records, using an integer data to record the selection status of an entry.



Content:

First, the table structure:

Two table



Item Item table

Key:id

Field Name
Type
Length
Default
Description

Id
Int
4

ID (AutoNumber)

ItemName
Nvarchar
50
0
Entry name










UserInfo User Information table

Key:userid

Field Name
Type
Length
Default
Description

Userid
Int
4

ID (AutoNumber)

Itemrecord
Int
4
0
Entry Selection Record




A note: Our goal is to put the user on the item all the records of the selection of records, after a certain amount of processing stored in the Itemrecord.

Second, the Data layer class:

Four: Quickitem, Quickitemcollection, UserInfo, userinfocollection

A little note:

1, Quicitem: The Item Information Processing class, contains four kinds of methods, namely the data Tim, delete, change, read.

2, Quickitemcollection: This is a query class, according to certain conditions read the collection of entries.

3, UserInfo: The user Information Processing class, contains four kinds of methods, namely the data Tim, delete, change, read.

4, Userinfocollection: This is a query class, according to certain conditions to read the collection of user information.

Third, the presentation layer call processing:



Get information on individual selected items

UserInfo UI = new UserInfo ();

ui.id=1;

Ui. Getinfobyid ();

int Itemrecord=ui. Itemrecord;



Get an array of IDs for all items

Quickitemcollection QIC = new Quickitemcollection ();

Qic. Getallinfo ();

int QICCOUNT=QIC. Count;

String Itemidarraystring=null;

for (int i=0;i<qiccount;i++)

{

itemidarraystring+= "," +qic[i].id;

}

Itemidarraystring=itemidarraystring.substring (1,itemidarraystring.length-1);

char[] dot = new char[]{', '};

string[] Itemidarray = Itemidarraystring.split (dot);





Call method returns an array of IDs for selected items

string [] Selectitemarray=selectitemarray (Itemidarray,itemrecord);



Note: The above code is to read the entry information and personal settings for the corresponding processing, Selectitemarray is a private method, the code is as follows:



#region Get the selected item recordset

<summary>

Get the selected item recordset

</summary>

<param name= "Tempitemidarray" > All project Recordsets </param>

<param name= "Tempitemrecord" > Project selection Information </param>

<returns> Select Project Recordset </returns>

Private string[] Selectitemarray (string[] tempitemidarray,int Tempitemrecord)

{

Number of ID array elements for the project

int itemidarraycount=tempitemidarray.length;

Convert plastic records into binary

String itemrecordstring = Convert.ToString (tempitemrecord,2);

Number of selected information bits stored

int itemrecordstringcount=itemrecordstring.length;

Number that needs to be mended 0

int addzerocount=itemidarraycount-itemrecordstringcount;

Start with a 0 high.

for (int i=0;i<addzerocount;i++)

{

itemrecordstring= "0" +itemrecordstring;

}



Select ID string

String Selectitemstring=null;

Start matching selected items

int count=itemrecordstring.length;

for (int j=0;j<count;j++)

{

if (Int32.Parse (itemrecordstring.substring (j,1)) ==1)

{

selectitemstring+= "," +tempitemidarray[j];

}

}

Shave the first ","

Selectitemstring=selectitemstring.substring (1,selectitemstring.length-1);

char[] dot = new char[]{', '};

Returns an array of selected project IDs

return selectitemstring.split (dot);

}

#endregion

A little note:

If you haven't read the above code, look at me now to explain it to you. The solution here is to use the binary data bits "0" "1" to identify whether or not to select a purpose, if selected is "1", each entry corresponds to a bits, if there are five pieces of data in the item, then all is "11111", such binary data, if we want to select the first, The second and fifth data, then the corresponding binary code is represented as "10101", and then we convert this binary data to decimal data: 16+0+4+0+1=21, so we can record various combinations of records by converting them. Note here is the question of the new entry, what if the item now becomes seven? Do not worry, become seven, then there are seven bit "1111111", then this time my original selection of three data is not to change, this increase of two bit is increased in the high, that is, now the storage representation into the "0010101" is: 0+0+16+0+4+0+1= 21, the value is unchanged. In general, we are adding entries, rarely reduce the situation of the entry, if you want to reduce the entry, you must write a program to reset the value of the Itemrecord.

So how do you reset the Itemrecord value after the user selects the entry again? Listen to me slowly, like this multiple selection of items are usually bound by CheckBoxList to select operations, we are the object of the element selection process, the code fragment as follows:

int itemcount=checkboxlist_item.items.count;

for (int i=0;i<itemcount;i++)

{

if (checkboxlist_item.items[i). Selected)

{

itemselect=itemselect+ "1";

}

Else

{

itemselect=itemselect+ "0";

}

}

int Itemrecord=convert.toint32 (ItemSelect, 2);



One point: First get the number of entries, and then do the loop, to determine whether the selected, if selected then the corresponding bit value is "1", otherwise "0", combining the binary string and then make a conversion, and then warehousing on all the finished.

Is it convenient? Not only is the convenience of this storage efficiency very high, if you have hundreds of entries to choose from, this scheme is a good choice, but if you really get hundreds of entries let others choose, unless it is forced to cope with the test, otherwise will encounter strong contempt, who has the patience to do this choice, haha!

Don't worry, give a reminder to see, do not make the order wrong, binding checkboxlist, in accordance with the order of DESC to bind elements list, otherwise, ho ho ...



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.