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 ;}
}
}
}
}