Share a copy of C # Database persistence layer framework design ideas and some of its code (original)

Source: Internet
Author: User

Development Background:

C #It is undoubtedly very convenient to access the database,SQL helper, a database volume type provided by Microsoft, allows us to easily perform database operations, compared with C ++.,JavaBut in a project, we usually need to write a lotSQLStatements, add, modify, and delete the most basicSQLWrite and

Not complex, but even those who have years of work experience are still complicated.SQLStatements are often prone to errors. We can't help but wonder if there is one that can not be spliced.SQL?

 

Ak2.0Design Concept:

As an excellentProgramPersonnel is to turn their ideasCodeTo achieve this, we want to do it, even if it fails, we will not regret anything, then let's talk about the ideas below:

For example, insert into tablea values (, 3)What is the simplest call method we can think? That's all.Insert the most basic codeOur developers don't have to worry about it. They can directly pass the table name and value. In other words, we only need to create an object and then pass the table name and the field to be inserted.

Our program is much simpler, of course.Update,DeleteAndSelectThey are all the same

 

Ak2.0Introduction:

Ak2.0Developed independently for me and their teamsThe database persistence layer framework goes through2Months of development, more than half a year of testing, is currently stable, the version is also from1.0Upgrade2.0Which implements the underlyingSQLAutomatic splicing, automatic processing, and no nonsense. We can directly paste some code, hoping to give you some inspiration:

We need to write a query statement: Select ID, name from tablea where id = xxx, then the following is the call method using ak2.0:

Public datetable gettableafeild (int id)

{

Idboprate DBO = new selectsql ("connlinkqpacc ");

DBO. tablename = "tablea ";

DBO. addfeild ("ID", null );

DBO. addfeild ("name", null );

DBO. addwhere ("ID", ID, "= ");

DBO. runsql ();

Return DBO. DT;

}

Parameter Introduction: connect to the database (connlinkqpacc), what operations to perform (selectql), the table (tablea), and the field (ID, name) the query condition (ID = xxx) must be supported across databases. Therefore, the parameter is passed directly during the call. It may be a bit difficult for you to see it for the first time, then, let's continue.

 

We need to write a statement for modification: Update tablea set Pwd = 'dss', name = 'zhangsan' where id = xxx, then the following is the call method using ak2.0:

Public int updtaetableafeild (string PWD, string name, int ID)

{

Idboprate DBO = new updatesql ("connlinkqpacc ");

DBO. tablename = "tablea ";

DBO. addfeild ("PWD", PWD );

DBO. addfeild ("name", name );

DBO. addwhere ("ID", ID, "= ");

Return DBO. runsql ();

}

Parameter Introduction: connect to the database (connlinkqpacc), what operations to perform (updatesql), the table (tablea), the Field (ID, name), and the value (PWD, name), what are the query conditions (ID = xxx)

 

If you delete and add Code directly, you will not be able to explain it. See if you can understand it:

Public int deletetableafeild (string PWD, string name, int ID)

{

Idboprate DBO = new deletesql ("connlinkqpacc ");

DBO. tablename = "tablea ";

DBO. addwhere ("ID", ID, "= ");

Return DBO. runsql ();

}

 

Public int inserttableafeild (string PWD, string name)

{

Idboprate DBO = new insertsql ("connlinkqpacc ");

DBO. tablename = "tablea ";

DBO. addfeild ("PWD", PWD );

DBO. addfeild ("name", name );

Return DBO. runsql ();

}

 

What do you feel after reading all this? Even for a newbie, this method is very easy to accept. So OK, this is what we want to achieve.

Additionally, idboprate is a factory model, where insertsql and updtaesql are all produced. For details, see the factory design model.

We welcome your valuable comments. We would like to thank two developers who have contributed to this version: klj and mrq.

The above is purely a personal opinion. For more information, see the source. -------------- AK: 2012-07-02

 

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.