예제 #1
0
        /// <summary>
        /// 立即执行更新语句
        /// </summary>
        /// <typeparam name="TModel">模型名称</typeparam>
        /// <param name="objExp">更新对象表达式,表示要更新的列信息</param>
        /// <returns>将返回一个操作接口,此接口会有一个Where方法和Go方法,Where表示要添加条件,Go则表示立即执行语句。</returns>
        /// <example> session.Update(u => new AdminUser1 {ID = "5", NameA = "maxzhang"}).Where(p => p.Age > 5).Go</example>
        public IOperatorWhere <TModel> Update <TModel>(Expression <Func <TModel, object> > objExp) where TModel : DbObject
        {
            var            tableName = DbMetaDataManager.GetTableName(typeof(TModel));
            FieldsFormater format    = new FieldsFormater(Provider);

            format.Visit(objExp);
            string paramterNameAndValues = string.Join(",", format.Parameters.Select(kv => kv.Key + "=" + kv.Value.Name));

            string template = string.Format("Update {0} SET {1} Where 1=1", tableName, paramterNameAndValues);
            var    ps       = format.Parameters.Values.Where(p => p.IsMethodType == false).ToList();

            return(new OperatorWhereObject <TModel>(Provider, template, ps));
        }
예제 #2
0
        /// <summary>
        /// 立即执行插入语句
        /// </summary>
        /// <typeparam name="TModel">模型名称</typeparam>
        /// <param name="objExp">插入对象表达式,表达要插入的列信息</param>
        /// <example> session.Insert(u => new AdminUser1 {ID = "5", NameA = "maxzhang"}).Where(p => p.Age > 5).Go;</example>
        public void Insert <TModel>(Expression <Func <TModel, object> > objExp) where TModel : DbObject
        {
            var            tableName = DbMetaDataManager.GetTableName(typeof(TModel));
            FieldsFormater format    = new FieldsFormater(Provider);

            format.Visit(objExp);
            string fieldNames    = string.Join(",", format.Parameters.Keys);
            string paramterNames = string.Join(",", format.Parameters.Values.Select(p => p.Name));
            string template      = string.Format("INSERT INTO {0}({1}) VALUES({2})", tableName, fieldNames, paramterNames);
            var    ps            = format.Parameters.Values.Where(p => p.IsMethodType == false).ToList();
            var    command       = new Command(template, ps);

            Provider.Execute(command);
        }