接口基类

    /// <summary>
    /// 接口基类
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public interface IBaseAccess<T> where T : class
    {
        int Add(T obj);
        int Update(T obj);
        T GetModel(T obj);
        int Delete(T obj);
        List<T> GetList();
    }

 

public abstract class IPager<T> where T : class, new()
    {
        /// <summary>
        /// 获取从读取流转换为实体对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        #region DoTransferType
        public T DoTransferType(IDataReader dr)
        {
            T model = new T();
            int count = dr.FieldCount;

            PropertyInfo[] property_lst = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in property_lst)
            {
                for (int i = 0; i < count; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        string name = dr.GetName(i).ToUpper();
                        if (name.Equals(property.Name.ToUpper()))
                        {
                            property.SetValue(model, dr.GetValue(i), null);
                            break;
                        }
                    }

                }
            }
            return model;

        }
        #endregion

        /// <summary>
        /// 获取从实体对象转换参数集合
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>泛型参数集合</returns>
        #region DoTransferParameter
        public List<SqlParameter> DoTransferParameter(T model)
        {
            List<SqlParameter> arylst = new List<SqlParameter>();
            PropertyInfo[] property_arylst = model.GetType().GetProperties();
            foreach (PropertyInfo info in property_arylst)
            {
                if (info.GetValue(model, null) != null
                    && !info.PropertyType.FullName.ToUpper().Contains("Entity"))
                {
                    string value = info.GetValue(model, null).ToString();
                    SqlParameter para = new SqlParameter("@" + info.Name, info.PropertyType.FullName);
                    para.Value = value;
                    arylst.Add(para);
                }
            }
            return arylst;

        }
       }

 

 [Serializable]实体中要写这样子的 标示 和数据库要对应

 
        public static int ExecuteNonQueryExpand(string connectionString, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                return ExecuteNonQueryExpand(connection, commandType, commandText, commandParameters);
            }
        }

 

 public static int ExecuteNonQueryExpand(SqlConnection connection, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
        {
            SqlCommand command = new SqlCommand();
            PrepareCommandExpand(command, connection, null, commandType, commandText, commandParameters);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            return num;
        }

 

    private static void PrepareCommandExpand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
        {
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            command.Connection = connection;
            command.CommandText = commandText;
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
            command.CommandType = commandType;
            if (commandParameters != null)
            {
                AttachParameters(command, commandParameters);
            }
        }

 

    private static void AttachParameters(SqlCommand command, List<SqlParameter> commandParameters)
        {
            foreach (SqlParameter parameter in commandParameters)
            {
                if ((parameter.Direction == ParameterDirection.InputOutput) && (parameter.Value == null))
                {
                    parameter.Value = DBNull.Value;
                }
                command.Parameters.Add(parameter);
            }
        }

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值