数据库助手类(可以处理实体类)

    在进行查询时使用sql语句非常灵活,但进行增删改操作时,对各个表的操作几乎相同,使用sql语句对各张表进行更新操作,显然有些冗余,为了开发方便,写了个DBHelper类来直接对实体类进行更新操作,代码如下:

 

代码
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>
        
/// 插入操作
        
/// </summary>
        
/// <param name="entityClass"></param>
        
/// <param name="flag">表是否是主键自增长</param>
        
/// <returns></returns>
        public int Insert(object entityClass, bool flag)
        {
            Type type 
= entityClass.GetType();
            
//获取属性集合
            PropertyInfo[] proInfo = type.GetProperties();
            
string items = string.Empty;
            
string itemValues = string.Empty;
            
foreach (PropertyInfo item in proInfo)
            {
                items 
+= ',';
                items 
+= item.Name;
                itemValues 
+= ',';
                
//判断是否为值引用
                if (item.PropertyType.IsValueType)
                {

                    itemValues 
+= item.GetValue(entityClass, null);
                }
                
else
                {
                    itemValues 
+= (string.Format("'{0}'", item.GetValue(entityClass, null)));
                }
            }
            
//移除前面的逗号至第二个逗号的位置
            if (flag)
            {
                items.Remove(
0, items.IndexOf(','1));
                itemValues.Remove(
0, itemValues.IndexOf(01));
            }
            
//移除第一个逗号
            else
            {
                items.Remove(
01);
                itemValues.Remove(
01);
            }
            
string sql = string.Format("Insert into {0}({1}) values({1})", type.Name, items, itemValues);
            
return dbHelper.ExcuteQuery(sql);
        }
        
        
/// <summary>
        
/// 更新操作
        
/// </summary>
        
/// <param name="entityClass">实体类</param>
        
/// <param name="flag">主键是否为自增长</param>
        
/// <returns></returns>
        public int Update(object entityClass, bool flag)
        {
            Type type 
= entityClass.GetType();
            
//获取属性集合
            PropertyInfo[] proInfo = type.GetProperties();
            
string items = string.Empty;
            
string condition = string.Empty;
            
foreach (PropertyInfo item in proInfo)
            {
                
//判断是否为值引用
                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);
            
//移除前面的逗号至第二个逗号的位置
            if (flag)
            {
                items.Remove(
0, items.IndexOf(','1));
                itemValues.Remove(
0, itemValues.IndexOf(01));
            }
            
//移除第一个逗号
            else
            {
                items.Remove(
01);
                itemValues.Remove(
01);
            }
            
string sql = string.Format("update {0} set {1} where {2}", type.Name, items, condition);
            
return dbHelper.ExcuteQuery(sql);
        }
        
/// <summary>
        
/// 实体类
        
/// </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; }
            }
        }
    }
}

 

 

posted @ 2010-01-31 17:03  codingspace  阅读(530)  评论(2)    收藏  举报