/// <summary> /// 获取DataSet /// </summary> /// <param name="sql"></param> /// <param name="pars"></param> /// <returns></returns> public DataSet GetDataSetAll(string sql, params OracleParameter[] pars) { OracleConfig.SetParsName(pars); sql = OracleConfig.GetOracleSql(sql); ExecLogEvent(sql, pars, true); OracleDataAdapter _OracleDataAdapter = new OracleDataAdapter(sql, _OracleConnection); if (_tran != null) { _OracleDataAdapter.SelectCommand.Transaction = _tran; } if (IsGetPageParas) { SqlSugarToolExtensions.RequestParasToOracleParameters(_OracleDataAdapter.SelectCommand.Parameters); } _OracleDataAdapter.SelectCommand.CommandTimeout = this.CommandTimeOut; _OracleDataAdapter.SelectCommand.CommandType = CommandType; _OracleDataAdapter.SelectCommand.BindByName = true; if (pars != null) { _OracleDataAdapter.SelectCommand.Parameters.AddRange(pars); } DataSet ds = new DataSet(); _OracleDataAdapter.Fill(ds); if (IsClearParameters) { _OracleDataAdapter.SelectCommand.Parameters.Clear(); } ExecLogEvent(sql, pars, false); return(ds); }
/// <summary> /// 获取DataReader /// </summary> /// <param name="sql"></param> /// <param name="pars"></param> /// <returns></returns> public OracleDataReader GetReader(string sql, params OracleParameter[] pars) { OracleConfig.SetParsName(pars); sql = OracleConfig.GetOracleSql(sql); ExecLogEvent(sql, pars, true); OracleCommand OracleCommand = new OracleCommand(sql, _OracleConnection); OracleCommand.BindByName = true; OracleCommand.CommandType = CommandType; OracleCommand.CommandTimeout = this.CommandTimeOut; if (_tran != null) { OracleCommand.Transaction = _tran; } if (pars != null) { OracleCommand.Parameters.AddRange(pars); } if (IsGetPageParas) { SqlSugarToolExtensions.RequestParasToOracleParameters(OracleCommand.Parameters); } OracleDataReader OracleDataReader = OracleCommand.ExecuteReader(); if (IsClearParameters) { OracleCommand.Parameters.Clear(); } ExecLogEvent(sql, pars, false); return(OracleDataReader); }
/// <summary> /// 插入 /// 使用说明:sqlSugar.Insert(entity); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity">插入对象</param> /// <param name="isIdentity">该属性已经作废可以不填,主键是否为自增长,true可以不填,false必填</param> /// <returns></returns> public object Insert <T>(T entity, bool isIdentity = true) where T : class { Type type = entity.GetType(); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); var seqMap = OracleConfig.SequenceMapping; StringBuilder sbInsertSql = new StringBuilder(); List <OracleParameter> pars = new List <OracleParameter>(); var identities = SqlSugarTool.GetIdentitiesKeyByTableName(this, typeName); isIdentity = identities != null && identities.Where(it => it.Key.ToLower() == typeName.ToLower()).Count() > 0; //sql语句缓存 string cacheSqlKey = "db.Insert." + type.FullName; var cacheSqlManager = CacheManager <StringBuilder> .GetInstance(); //属性缓存 string cachePropertiesKey = "db." + type.FullName + ".GetProperties"; var cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance(); PropertyInfo[] props = null; if (cachePropertiesManager.ContainsKey(cachePropertiesKey)) { props = cachePropertiesManager[cachePropertiesKey]; } else { props = type.GetProperties(); cachePropertiesManager.Add(cachePropertiesKey, props, cachePropertiesManager.Day); } var isContainCacheSqlKey = cacheSqlManager.ContainsKey(cacheSqlKey); if (isContainCacheSqlKey) { sbInsertSql = cacheSqlManager[cacheSqlKey]; } else { //2.获得实体的属性集合 //实例化一个StringBuilder做字符串的拼接 sbInsertSql.Append("insert into " + typeName + " ("); //3.遍历实体的属性集合 foreach (PropertyInfo prop in props) { if (this.IsIgnoreErrorColumns) { if (!SqlSugarTool.GetColumnsByTableName(this, typeName).Any(it => it.ToLower() == prop.Name.ToLower())) { continue; } } //4.将属性的名字加入到字符串中 sbInsertSql.Append("" + prop.Name + ","); } //**去掉最后一个逗号 sbInsertSql.Remove(sbInsertSql.Length - 1, 1); sbInsertSql.Append(" ) values("); } //5.再次遍历,形成参数列表"(@xx,@xx@xx)"的形式 foreach (PropertyInfo prop in props) { //EntityState,@EntityKey if (!isIdentity || identities.Any(it => it.Value.ToLower() != prop.Name.ToLower())) { if (this.IsIgnoreErrorColumns) { if (!SqlSugarTool.GetColumnsByTableName(this, typeName).Any(it => it.ToLower() == prop.Name.ToLower())) { continue; } } if (!cacheSqlManager.ContainsKey(cacheSqlKey)) { sbInsertSql.Append(":" + prop.Name + ","); } object val = prop.GetValue(entity, null); if (val == null) { val = DBNull.Value; } if (_serialNumber.IsValuable()) { Func <PubModel.SerialNumber, bool> serEexp = it => it.TableName.ToLower() == typeName.ToLower() && it.FieldName.ToLower() == prop.Name.ToLower(); var isAnyNum = _serialNumber.Any(serEexp); if (isAnyNum && (val == DBNull.Value || val.IsNullOrEmpty())) { if (_serialNumber.First(serEexp).GetNumFunc != null) { val = _serialNumber.First(serEexp).GetNumFunc(); } if (_serialNumber.First(serEexp).GetNumFuncWithDb != null) { val = _serialNumber.First(serEexp).GetNumFuncWithDb(this); } } } if (prop.PropertyType.IsEnum) { val = (int)(val); } var par = new OracleParameter(":" + prop.Name, val.ToOracleParValue()); OracleConfig.SetParType(typeName, prop, par, this); SqlSugarTool.SetParSize(par); pars.Add(par); } else { if (!cacheSqlManager.ContainsKey(cacheSqlKey)) { var seqList = seqMap.Where(it => it.TableName.ToLower() == typeName.ToLower() && it.ColumnName.ToLower() == prop.Name.ToLower()); if (seqList.Any()) { var seqName = seqList.First().Value; sbInsertSql.Append(seqName + ".Nextval,"); } else { sbInsertSql.Append("null,"); } } } } if (!isContainCacheSqlKey) { //**去掉最后一个逗号 sbInsertSql.Remove(sbInsertSql.Length - 1, 1); sbInsertSql.Append(")"); cacheSqlManager.Add(cacheSqlKey, sbInsertSql, cacheSqlManager.Day); } var sql = sbInsertSql.ToString(); try { var lastInsertRowId = ExecuteCommand(sql, pars.ToArray()); if (lastInsertRowId > 0) { if (isIdentity) { var seqName = seqMap.First(it => it.TableName.ToLower() == typeName.ToLower()).Value; var eqValue = GetInt("SELECT " + seqName + ".currval from dual"); lastInsertRowId = eqValue; } else { lastInsertRowId = 1; } } return(lastInsertRowId); } catch (Exception ex) { throw new Exception("sql:" + sql + "\n" + ex.Message); } }