Example #1
0
        /// <summary>
        /// 生成插入语句
        /// </summary>
        /// <param name="model">模型对象</param>
        /// <returns></returns>
        public virtual string BuildInsertSQL(Model.Model model)
        {
            List<string> fileds = new List<string>();
            List<string> values = new List<string>();
            var propertys = model.GetType().GetProperties();
            var identity = model.getIdentify();
            foreach (var p in propertys)
            {
                if (!Magic.Mvc.Equals.IsNull(identity) && identity.Name == p.Name) continue;

                var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false);
                if (notmaps != null && notmaps.Length > 0) continue;

                fileds.Add(string.Format("[{0}]", p.Name));
                values.Add(string.Format("@{0}", p.Name));
            }

            return string.Format(" INSERT INTO [{0}]({1}) VALUES({2}) ", model.GetType().Name, string.Join(",", fileds.ToArray()), string.Join(",", values.ToArray()));
        }
Example #2
0
        /// <summary>
        /// 生成更新语句
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual string BuildUpdateSQL(Model.Model model)
        {
            List<string> fileds = new List<string>();
            List<string> wheres = new List<string>();
            var propertys = model.GetType().GetProperties();
            var primarys = model.getPrimaryKeys();
            var identity = model.getIdentify();
            foreach (var p in propertys)
            {
                var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false);
                if (notmaps != null && notmaps.Length > 0) continue;

                if (primarys.Any(m => m.Name == p.Name))
                {
                    wheres.Add(string.Format("[{0}][email protected]{0}", p.Name));
                }
                if (!Magic.Mvc.Equals.IsNull(identity) && identity.Name == p.Name) continue;

                fileds.Add(string.Format("[{0}][email protected]{0}", p.Name));

            }

            return string.Format(" UPDATE [{0}] SET {1} WHERE {2}", model.GetType().Name, string.Join(",", fileds.ToArray()), string.Join(" and ", wheres.ToArray()));
        }