Param/Parameter.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using DataObject.Attrib;
using DataObject.Dele;

namespace DataObject.Param
{
    /// <summary>
    /// 参数,属性设置
    /// </summary>
    public sealed class Parameter
    { 
        
        /// <summary>
        /// 实体转化成参数实例化
        /// </summary>
        /// <param name="info">需要转化的实体</param>
        public static IDataParameter[] GetParameter<T>(T info)
        {
            return GetParameter(new T[] { info }, false);
        }

        /// <summary>
        /// 实体数组转化成参数实例化
        /// </summary>
        /// <param name="infos">需要转化的实体</param>
        /// <param name="isOutput">是否要有输出参数</param>
        public static IDataParameter[] GetParameter<T>(T[] infos, bool isOutput)
        {
            List<SqlParameter> list = new List<SqlParameter>();
            SetItemT(infos, (c, obj) =>
            {
                if (list.Exists((sqlPar) => { return sqlPar.ParameterName == "@" + c.Name; })) return;
                //设置参数 
                if (isOutput && c.PrimaryKey)
                    list.Add(new SqlParameter("@" + c.Name, c.dbType, c.Size) { Value = GetParameterValue(obj ?? 0), Direction = ParameterDirection.InputOutput });
                else if (obj != null)
                    list.Add(new SqlParameter("@" + c.Name, c.dbType, c.Size) { Value = GetParameterValue(obj) });//实体对象情况
            });
            return list.ToArray<IDataParameter>();
        }

        /// <summary>
        /// 获取参数值
        /// </summary>
        /// <param name="obj">实体对象</param>
        /// <returns></returns>
        private static object GetParameterValue(object obj) {
            //判断对象是否是实体类
            if (obj.GetType().BaseType.Namespace != "System") {
                PropertyInfo pro = obj.GetType().GetProperties().First((p) =>{
                    object[] columns = p.GetCustomAttributes(typeof(DBColumn), true);
                    return columns.Length > 0 && (columns[0] as DBColumn).PrimaryKey;
                });
                return pro != null ? pro.GetValue(obj, null) : 0;
            }
后面还有 44 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。