/// <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 = ""; foreach (Attribute.FieldAttribute info in typeArry) { string name = info.Name; if (info.IsPrimaryKey) { continue; } 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},", name); helper.AddParam(name, value); } sql1 = sql1.Substring(0, sql1.Length - 1); sql2 = sql2.Substring(0, sql2.Length - 1); sql += sql1 + ") values( " + sql2 + ") ; SELECT scope_identity() ;"; sql = SqlFormat(sql); return(Convert.ToInt32(helper.ExecScalar(sql))); }
/// <summary> /// 指定替换对象返回单个结果 /// </summary> /// <param name="sql"></param> /// <param name="types">格式化SQL语句的关键类型</param> /// <returns></returns> public object ExecScalar(string sql, params Type[] types) { sql = AutoFormat(sql, types); sql = _DBAdapter.SqlFormat(sql); object obj = helper.ExecScalar(sql); ClearParame(); return(obj); }
internal static object ExecScalar(CoreHelper.DBHelper __DbHelper, string sql) { object obj = null; var el = Run(() => { obj = __DbHelper.ExecScalar(sql); }); Base.SaveSQLRunningtme(sql, el); return(obj); }
/// <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); }