Database Assistant class (entity class can be processed)

Source: Internet
Author: User

The SQL statement is flexible for queries, but the operations on each table are almost the same during addition, deletion, and modification. The SQL statement is obviously redundant when updating each table, for ease of development, a dbhelper class is written to directly update the object class,CodeAs follows:

 

Code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. reflection;

Namespace Codetwo
{
Public   Class Dal
{
Dbheper dbhelper =   New Dbheper ();
///   <Summary>
/// Insert operation
///   </Summary>
///   <Param name = "entityclass"> </param>
///   <Param name = "flag"> Whether the table is auto-incrementing by the primary key </Param>
///   <Returns> </returns>
Public   Int Insert ( Object Entityclass, Bool Flag)
{
Type type = Entityclass. GetType ();
// Get Attribute Set
Propertyinfo [] proinfo = Type. getproperties ();
String Items =   String . Empty;
String Itemvalues =   String . Empty;
Foreach (Propertyinfo item In Proinfo)
{
Items + =   ' , ' ;
Items + = Item. Name;
Itemvalues + =   ' , ' ;
// Judge whether it is a value reference
If (Item. propertytype. isvaluetype)
{

Itemvalues + = Item. getvalue (entityclass, Null );
}
Else
{
Itemvalues + = ( String . Format ( " '{0 }' " , Item. getvalue (entityclass, Null )));
}
}
// Remove the preceding comma to the second comma
If (FLAG)
{
Items. Remove ( 0 , Items. indexof ( ' , ' , 1 ));
Itemvalues. Remove ( 0 , Itemvalues. indexof ( 0 , 1 ));
}
// Remove the first comma
Else
{
Items. Remove ( 0 , 1 );
Itemvalues. Remove ( 0 , 1 );
}
String SQL =   String . Format ( " Insert into {0} ({1}) values ({1 }) " , Type. Name, items, itemvalues );
Return Dbhelper. excutequery (SQL );
}

///   <Summary>
/// Update operation
///   </Summary>
///   <Param name = "entityclass"> Entity class </Param>
///   <Param name = "flag"> Whether the primary key is auto-Increment </Param>
///   <Returns> </returns>
Public   Int Update ( Object Entityclass, Bool Flag)
{
Type type = Entityclass. GetType ();
// Get Attribute Set
Propertyinfo [] proinfo = Type. getproperties ();
String Items =   String . Empty;
String Condition =   String . Empty;
Foreach (Propertyinfo item In Proinfo)
{
// Judge whether it is a value reference
If (Item. propertytype. isvaluetype)
{

Itemvalues + =   String . Format ( " , {0 }={ 1} " , Item. Name, item. getvalue (entityclass, Null ));
}
Else
{
Itemvalues + =   String . Format ( " , {0} = '{1 }' " , Item. Name, item. getvalue (entityclass, Null ));
}
}
Condition = Items. substring ( 1 , Items. indexof ( ' , ' , 1 ) -   1 );
// Remove the preceding comma to the second comma
If (FLAG)
{
Items. Remove ( 0 , Items. indexof ( ' , ' , 1 ));
Itemvalues. Remove ( 0 , Itemvalues. indexof ( 0 , 1 ));
}
// Remove the first comma
Else
{
Items. Remove ( 0 , 1 );
Itemvalues. Remove ( 0 , 1 );
}
String SQL =   String . Format ( " Update {0} set {1} Where {2} " , Type. Name, items, condition );
Return Dbhelper. excutequery (SQL );
}
///   <Summary>
/// Entity class
///   </Summary>
Class Role
{
Int Roleid;
Public   Int Roleid
{
Get { Return Roleid ;}
Set {Roleid = Value ;}
}
String Names;
Public   String Names
{
Get { Return Names ;}
Set {Names = Value ;}
}
String Description;
Public   String Description
{
Get { Return Description ;}
Set {Description = Value ;}
}
}
}
}

 

 

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.