Exemplo n.º 1
0
        /// <summary>
        /// 获取update语句
        /// </summary>
        /// <param name="t">实体类型</param>
        /// <param name="conditionName">条件字段,默认为ID,如果有多个条件,可以用","隔开</param>
        /// <returns></returns>
        public string GetUpdateSql(T t, string conditionName = "ID")
        {
            try
            {
                string strTable     = ""; //表名
                string strSetValue  = ""; //更新的值
                string strCondition = ""; //条件

                //获取表名
                strTable = InternalBase <T> .GetTableName();

                PropertyInfo[] pi = t.GetType().GetProperties();
                bool           isCondition;//此属性是否为条件
                foreach (var item in pi)
                {
                    //todo:想使用特性,来判断是否把属性赋空值,但是没好用.目前的问题就是,无法把非string类型的值修改为空
                    //string columnType = string.Empty;
                    //object[] propertyAttrs = item.GetCustomAttributes(false);
                    //for (int i = 0; i < propertyAttrs.Length; i++)
                    //{
                    //    object propertyAttr = propertyAttrs[i];
                    //    //获取Column自定义属性中配置的type值(表的列名)
                    //    columnType = GetColumnType(propertyAttr);
                    //}

                    isCondition = false;
                    string key   = item.Name;
                    object value = item.GetValue(t, null);
                    if (value == null)//&& string.IsNullOrEmpty(columnType)
                    {
                        continue;
                    }
                    string[] arrayCondition = conditionName.Split(',');
                    foreach (var condition in arrayCondition)
                    {
                        if (key.ToUpper().Equals(condition.ToUpper()))
                        {
                            strCondition += string.Format(" and {0}='{1}'", key, value);
                            isCondition   = true;
                            break;
                        }
                    }
                    if (isCondition)
                    {
                        continue;
                    }
                    else
                    {
                        if (value.GetType() == typeof(DateTime))
                        {
                            strSetValue += string.Format(",{0}={1}", key, "to_date('" + value + "','yyyy/mm/dd hh24:mi:ss')");
                        }
                        else
                        {
                            strSetValue += string.Format(",{0}='{1}'", key, value);
                        }
                    }
                }
                if (strCondition.Length > 0)
                {
                    strCondition = InternalBase <T> .IsKeepAndWhere(strCondition, false, true);
                }
                return(string.Format("UPDATE {0} SET {1} {2}", strTable, strSetValue.Substring(1), strCondition));
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                return("-1");
            }
        }