泪牛满面,终于成功鸟!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Blog.Models.Interfaces;
using System.Reflection;
using System.Configuration;
using Blog.Code;
namespace Blog.Models.MockModels
{
    public class MDalBase<T> : IDalBasecs<T>
    {
        private string tablename;
        #region IDalBasecs<T> 成员
        public bool One(T value)
        {
            throw new NotImplementedException();
        }
        public MDalBase(string tablename)
        {
            this.tablename = tablename;
        }
        public void Add(T t)
        {
            PropertyInfo[] infos = t.GetType().GetProperties();
            string s = "insert " + tablename;
            string propertyinfo = "(";
            string values = "values(";
            for (int i = 0; i < infos.Length - 1; i++)
            {
                propertyinfo += infos[i].Name + ",";
                values += "'" + infos[i].GetValue(t, null) + "',";
            }
            propertyinfo += infos[infos.Length-1].Name;
            values += "'" + infos[infos.Length - 1].GetValue(t, null) + "'";
            propertyinfo += ")";
            values += ")";
            s += propertyinfo + values;
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, s, null);
        }///测试通过

        public void Remove(Guid id)
        {
            string s = "delete form " + tablename;
            s += " where id =";
            s += "'" + id.ToString() + "'";
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, s, null);
        }///未测试

        public void Update(T value)
        {
            string s = "update " + tablename;
            s += " set ";
            string propertyinfo = "";
            PropertyInfo[] infos = value.GetType().GetProperties();
            for (int i = 0; i < infos.Length - 1; i++)
            {
                propertyinfo += infos[i].Name + "=";
                propertyinfo += "'" + infos[i].GetValue(value, null) + "',";
            }
            propertyinfo += infos[infos.Length - 1].Name + "=";
            propertyinfo += "'" + infos[infos.Length - 1].GetValue(value, null) + "'";
            s += propertyinfo;
            s += " where id = '" + infos[0].GetValue(value, null) + "'";
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, s, null);
        }///未测试。。。。

        public T Select(Guid id)
        {
            T o = Activator.CreateInstance<T>();///必须开放默认无参数构造器
            string s = "select * from " + tablename;
            s += " where id = '" + id.ToString() + "'";
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, s, null);
            while (dr.Read())
            {
                foreach (PropertyInfo Property in typeof(T).GetProperties())
                {
                    if (Property.PropertyType == typeof(Guid))
                    {
                        Property.SetValue(o, new Guid(dr[Property.Name].ToString()), null);
                    }
                    else
                    {
                        Property.SetValue(o, Convert.ChangeType(dr[Property.Name].ToString(), Property.PropertyType), null);
                    }
                }
            }
            return o;
        }///测试通过

        public List<T> Fill()
        {
            List<T> DataList = new List<T>();
            string s = "select * from " + tablename;
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, s, null);
            while (dr.Read())
            {
                T RowInstance = Activator.CreateInstance<T>();
                foreach (PropertyInfo Property in typeof(T).GetProperties())
                {
                    if (Property.PropertyType == typeof(Guid))
                    {
                        Property.SetValue(RowInstance, new Guid(dr[Property.Name].ToString()), null);
                    }
                    else
                    {
                        Property.SetValue(RowInstance, Convert.ChangeType(dr[Property.Name].ToString(), Property.PropertyType), null);
                    }
                }
                DataList.Add(RowInstance);
            }
            return DataList;
        }///测试通过

        #endregion
    }
}

DLLBASE。定义所有数据库与实体类间的五个常用操作。。。用了泛型,映射。不过效率肯定大大的有问题的。不过可以容忍了,毕竟只是小笔操作。

 

 

posted @ 2010-02-26 22:47  飘扬  阅读(276)  评论(0编辑  收藏  举报