Improvement of one data access layer

Source: Internet
Author: User
With the advent of the Internet era for civilians, micro-databases must play their unique roles. Access is a good choice! Today, I also used the Access database. Although it is small, there are many names! Query is always the same as God's problem. It keeps exploring and never ends up approaching the limit. Obviously, how can we use the least code to write the best query? Yes, the question is: How

With the advent of the civilian network era, the micro-data library must play its unique role. Access is a good choice! Today, I also used the Access data library. Although it is small, it has many names! Query is always the same as God's problem. It keeps exploring and never ends up approaching the limit. Obviously, how can we use the least code to write the best query? Yes, the question is: How

With the advent of the civilian network era, microDataLibrary must play its unique role, and Access is a good choice!

Today, I also used AccessDataLibrary, although the sparrow is small, there are many names!

Query is always the same as God's problem. It keeps exploring and never ends up approaching the limit. Obviously, how can we use the least code to write the best query?

Yes. The problem is:

How to write the best query with the least code?

My definition of "best" is: code is the most beautiful, efficiency is the most appropriate (can be fast, can not be enough quickly enough on the line), the fastest development speed.

Here, the user logon is used as an example to convert the corresponding entity class (UserLoginInfo. cs ):

UserLoginInfo. cs

Next let's play togetherDataUpdated game query:

Specific problem: the user with the userPass "123456" password is found

1. Original Query

GetList (string userPass)

// A sentence is missing

Foreach (OleDbParameter para in paras ){

Cmd. Parameters. Add (para );

}

Conclusion: At this stage, the most disgusting thing is

1. A large number of Connection and Command operations

2. Read the UserLoginInfo process assigned by reader to the list. Every field and index pair are upset!

2. encapsulation of Connection and Command.

It's just like SqlHelper. You can just create one.

ACECommonHelper

The general idea is that calling code can also be imagined. The code can be reused by referring to the next step.

3.ImprovementReader fills in the list and is automatically read.

Read data based on reflectionDataThe fields in the library are automatically assigned values to the fields corresponding to UserLoginInfo.

Note: Because the code is generated,DataThe fields of the library are basically the same as the attributes of the generated class.

The following classes are available on the Internet:

ACEReaderToMode. cs

  ImprovementQuery Method:

GetList (string userPass)

Public List GetList (string userPass)

{

List List = null;

List Paras = new List ();

OleDbParameter para = new OleDbParameter ("@ userPass", OleDbType. LongVarWChar );

Para. Value = userPass;

Paras. Add (para );

Using (OleDbConnection connection = new OleDbConnection (connectionString ))

{

OleDbDataReader reader = AccessHelper. ACECommonHelper. ExcuteReader (

"Select * from User_login where ",

Conn,

Paras. ToArray ());

List = AccessHelper. ACEReaderToModel. ReaderToList (Reader );

Reader. Close ();

Conn. Close ();

}

Return list;

}

[1] [2] Next page

[Editor: coco]

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.