现在的位置: 首页 > 综合 > 正文

用反射写个orm试试

2013年10月06日 ⁄ 综合 ⁄ 共 5470字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace VO
{
    /// <summary>
    /// ORM测试
    /// </summary>
    public class ORM
    {
        private SqlConnection sqlConn;

        public ORM()
        {
            string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["WJKUConnectionString"].ToString();
            sqlConn = new SqlConnection(ConnString);
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        public ORM Test(string[] strTest)
        {
            return this;
        }

        /// <summary>
        /// 全字段插入
        /// </summary>
        /// <returns></returns>
        public Boolean Save()
        {
            return executeSave(makeInsert(getClassPropertyInfo(this), this));
        }

        /// <summary>
        /// 筛选字段插入
        /// </summary>
        /// <param name="strNoInsert">需要排除不插入的字段</param>
        /// <returns></returns>
        public Boolean Save(string strNoInsert)
        {
            return executeSave(makeInsert(getClassPropertyInfo(this), this, strNoInsert));
        }

        public Boolean executeSave(string strSql)
        {
            SqlCommand sqlCom = new SqlCommand(strSql, sqlConn);
            sqlConn.Open();
            int i=sqlCom.ExecuteNonQuery();
            sqlConn.Close();
            if (i > 0) { return true; } else { return false; }
        }

        /// <summary>
        /// 取得插入的SQL语句
        /// </summary>
        /// <returns></returns>
        public string getInsertSQL()
        {
            return makeInsert(getClassPropertyInfo(this), this);
        }

        /// <summary>
        /// 取得插入的SQL语句,排除参数中的字段
        /// </summary>
        /// <param name="strNoInsert"></param>
        /// <returns></returns>
        public string getInsertSQL(string strNoInsert)
        {
            return makeInsert(getClassPropertyInfo(this), this, strNoInsert);
        }

        /// <summary>
        /// 组装插入全字段
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="ob"></param>
        /// <returns></returns>
        public String makeInsert(PropertyInfo[] propertyInfo, object ob)
        {
            string strClassName = makeClassName(ob.GetType().Name);
            string strSQL = "insert into dbo." + strClassName;
            string strField = " (";
            string strValue = " values (";
            for (int i = 0; i < propertyInfo.Length; i++)
            {
                if (i < propertyInfo.Length - 1)
                {
                    strField = strField + propertyInfo[i].Name + ",";
                    strValue = strValue + makeValue(propertyInfo[i].PropertyType, propertyInfo[i].GetValue(ob, null)) + ",";
                }
                else
                {
                    strField = strField + propertyInfo[i].Name;
                    strValue = strValue + makeValue(propertyInfo[i].PropertyType, propertyInfo[i].GetValue(ob, null));
                }
            }
            strSQL = strSQL + strField + ")" + strValue + ")";
            return strSQL;
        }

        /// <summary>
        /// 组装插入非全字段
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="ob"></param>
        /// <param name="strNoInsert"></param>
        /// <returns></returns>
        public String makeInsert(PropertyInfo[] propertyInfo, object ob, string strNoInsert)
        {
            string strClassName = makeClassName(ob.GetType().Name);
            string strSQL = "insert into dbo." + strClassName;
            string strField = "(";
            string strValue = " values(";
            for (int i = 0; i < propertyInfo.Length; i++)
            {
                if (i < propertyInfo.Length - 1)
                {
                    if (propertyInfo[i].Name.ToString().ToLower() != strNoInsert.ToLower())
                    {
                        strField = strField + propertyInfo[i].Name + ",";
                        strValue = strValue + makeValue(propertyInfo[i].PropertyType, propertyInfo[i].GetValue(ob, null)) + ",";
                    }

                }
                else
                {
                    if (propertyInfo[i].Name.ToString().ToLower() != strNoInsert.ToLower())
                    {
                        strField = strField + propertyInfo[i].Name;
                        strValue = strValue + makeValue(propertyInfo[i].PropertyType, propertyInfo[i].GetValue(ob, null));
                    }

                }
            }
            strSQL = strSQL + strField + ")" + strValue + ")";
            return strSQL;
        }

        /// <summary>
        /// 对值进行分类组装
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="ob"></param>
        /// <returns></returns>
        public string makeValue(Type tp, object ob)
        {
            if (tp == typeof(string))
            {
                return "'" + ob + "'";
            }
            else if (tp == typeof(int))
            {
                return "" + ob;
            }
            return "" + ob;
        }

        /// <summary>
        /// 映射的表名
        /// </summary>
        /// <param name="strClassName"></param>
        /// <returns></returns>
        public string makeClassName(string strClassName)
        {
            int i = strClassName.ToUpper().LastIndexOf("VO");
            if (i >= 0)
            {
                return strClassName.Substring(0, i);
            }
            else
            {
                return strClassName;
            }
        }

        /// <summary>
        /// 取得属性信息
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        public PropertyInfo[] getClassPropertyInfo(Object ob)
        {
            Type t = ob.GetType();
            Console.Write(ob.ToString());
            PropertyInfo[] propertyInfo = t.GetProperties();
            return propertyInfo;
        }
    }
}

抱歉!评论已关闭.