Ejemplo n.º 1
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="helper"></param>
        /// <returns></returns>
        public override int InsertObject(IModel obj, CoreHelper.DBHelper helper)
        {
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type);
            var    typeArry = TypeCache.GetProperties(type, true).Values;
            string sql      = string.Format("insert into {0}(", table);
            string sql1     = "";
            string sql2     = "";

            string sequenceName = string.Format("{0}_sequence", table);
            var    sqlGetIndex  = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句
            int    id           = Convert.ToInt32(helper.ExecScalar(sqlGetIndex));

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                string name = info.Name;
                if (info.IsPrimaryKey)
                {
                    //continue;//手动插入ID
                }
                if (!string.IsNullOrEmpty(info.VirtualField))
                {
                    continue;
                }
                object value = info.GetValue(obj);
                value = ObjectConvert.SetNullValue(value, info.PropertyType);
                sql1 += string.Format("{0},", info.KeyWordName);
                sql2 += string.Format("@{0},", info.KeyWordName);
                helper.AddParam(info.KeyWordName, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ")";
            sql  = SqlFormat(sql);
            var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;

            helper.SetParam(primaryKey.Name, id);
            helper.Execute(sql);
            //var helper2 = helper as CoreHelper.OracleHelper;
            //int id = helper2.Insert(sql,sequenceName);
            return(id);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 设置参数
 /// </summary>
 /// <param name="name"></param>
 /// <param name="value"></param>
 public void SetParam(string name, object value)
 {
     value = ObjectConvert.SetNullValue(value);
     helper.SetParam(name, value);
 }