Пример #1
0
        /// <summary>
        /// 将实体对象转换成SqlParameter[]
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="pis"></param>
        /// <returns></returns>
        public static SqlParameter[] GetParameters(object obj, PropertyInfo [] pis = null)
        {
            List <SqlParameter> listParams = new List <SqlParameter>();

            if (obj != null)
            {
                var type  = obj.GetType();
                var isDic = type.IsIn(SqlSugarTool.DicArraySO, SqlSugarTool.DicArraySS);
                if (isDic)
                {
                    if (type == SqlSugarTool.DicArraySO)
                    {
                        var newObj = (Dictionary <string, object>)obj;
                        var pars   = newObj.Select(it => new SqlParameter("@" + it.Key, it.Value));
                        foreach (var par in pars)
                        {
                            SetParSize(par);
                        }
                        listParams.AddRange(pars);
                    }
                    else
                    {
                        var newObj = (Dictionary <string, string>)obj;
                        var pars   = newObj.Select(it => new SqlParameter("@" + it.Key, it.Value));
                        foreach (var par in pars)
                        {
                            SetParSize(par);
                        }
                        listParams.AddRange(pars);;
                    }
                }
                else
                {
                    PropertyInfo[] propertiesObj = null;
                    if (pis != null)
                    {
                        propertiesObj = pis;
                    }
                    else
                    {
                        propertiesObj = type.GetProperties();
                    }
                    string replaceGuid = Guid.NewGuid().ToString();
                    foreach (PropertyInfo r in propertiesObj)
                    {
                        var value = r.GetValue(obj, null);
                        if (r.PropertyType.IsEnum)
                        {
                            value = Convert.ToInt64(value);
                        }
                        if (value == null || value.Equals(DateTime.MinValue))
                        {
                            value = DBNull.Value;
                        }
                        if (r.Name.ToLower().Contains("hierarchyid"))
                        {
                            var par = new SqlParameter("@" + r.Name, SqlDbType.Udt);
                            par.UdtTypeName = "HIERARCHYID";
                            par.Value       = value;
                            listParams.Add(par);
                        }
                        else
                        {
                            var par = new SqlParameter("@" + r.Name, value);
                            SetParSize(par);
                            if (value == DBNull.Value)
                            {//防止文件类型报错
                                SqlSugarTool.SetSqlDbType(r, par);
                            }
                            listParams.Add(par);
                        }
                    }
                }
            }
            return(listParams.ToArray());
        }