public static async Task <ExecAsyncResult> ExecDataReadByInheritanceAsync <T>(this DataSItem item, DbAgent agent) where T : DbObject { //return Task.Factory.StartNew(() => // { // ExecAsyncResult result = new ExecAsyncResult(); // IBag<T> container; // ExecResult rs = ExecDataReadByInheritance<T>(item, agent, out container); // result.ExecutedProcedure = item; // result.Result = rs; // result.Object = container; // result.ExecutionType=AsyncExecutionType.ExecByINheritance; // return result; // }); ExecAsyncResult result = new ExecAsyncResult(); result.ExecutedProcedure = item; IBag <T> container; if (agent != null && agent.State) { // return ExecuteProcedureDRByReflection<T>(item, out container, agent.ConnectionString); //--- SqlCommand cmd = null; container = new Bag <T>(); result.StartMeasure(); try { // create and open a connection object "Data Source=FARID-PC;Initial Catalog=InsuranceFactory;Integrated Security=True" //using (_conn = new SqlConnection(constr)) //{ SqlConnection con = null; if (agent.ConnectionLevel == ConnectionLevel.Single) { con = agent.CreateConnection(); await con.OpenAsync(); } else if (agent.ConnectionLevel == ConnectionLevel.AllInOne) { con = agent.Connection; } // check state of connection cmd = new SqlCommand(item.Value, con); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandTimeout = agent.RunTimeout; if (agent.TransactionState == TransactionState.ActiveTransaction) { cmd.Transaction = agent.Transaction; } SqlParameter param; foreach (var itm in item.Params.Values) { param = new SqlParameter(); param.ParameterName = itm.Name; param.Value = itm.Value; param.SqlDbType = SettingsHelperManager.DetermineSqlDbTYpe(itm.Type); param.Direction = SettingsHelperManager.GetParametrDirection(itm.Direction); cmd.Parameters.Add(param); } using (_reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false)) { while (await _reader.ReadAsync().ConfigureAwait(false)) { container.SetFromReader(ref _reader); } } //set outputparams values if (item.HasOutputParam) { foreach (DataParam value in item.OutputParams.Values) { value.Value = cmd.Parameters[value.Name].Value.ToString(); } } if (cmd.Parameters.Count > 0) { List <DataParam> ret = item.GetparamsByDirection(ParamDirection.Return); if (ret.Count > 0) { if (cmd.Parameters[ret[0].Name] != null) { result.Result.SetCode( Convert.ToInt32( cmd.Parameters[item.GetparamsByDirection(ParamDirection.Return)[0].Name].Value)); } } } // }end of using if (agent.ConnectionLevel == ConnectionLevel.Single && con != null) //&& agent.AgentState != AgentState.Disconnected) { con.Dispose(); } result.Object = container; } catch (Exception exc) { throw exc; } result.StopMeasure(); result.ExecutionType = AsyncExecutionType.ExecByINheritance; return(result); //---- } else { throw new Exception("Agent is null or the state is false "); } }
/// <summary> /// </summary> /// <typeparam name="T"> Class type </typeparam> /// <param name="proc"> DataSettingItem </param> /// <param name="container">Bag container </param> /// <param name="datasource"></param> /// <returns>return value of procedure </returns> private static ExecResult ExecuteProcedureDR <T>(DataSItem proc, out IBag <T> container, DbAgent datasource) where T : DbObject { ExecResult result = new ExecResult(); SqlCommand cmd = null; result.StartMeasure(); container = new Bag <T>(); try { // create and open a connection object "Data Source=FARID-PC;Initial Catalog=InsuranceFactory;Integrated Security=True" SqlConnection con = null; if (datasource.ConnectionLevel == ConnectionLevel.Single) //&& datasource.AgentState != AgentState.Connected) { con = datasource.CreateConnection(); con.Open(); } else if (datasource.ConnectionLevel == ConnectionLevel.AllInOne) { con = datasource.Connection; } // return if is not connected if (con == null || con.State != ConnectionState.Open) { result.SetCode(-2, "Not connected ! "); result.StopMeashure(); return(result); } using (cmd = new SqlCommand(proc.Value, con)) { cmd.CommandTimeout = datasource.RunTimeout; //set transaction if (datasource.TransactionState == TransactionState.ActiveTransaction) { cmd.Transaction = datasource.Transaction; } cmd.CommandType = CommandType.StoredProcedure; SqlParameter param; foreach (var item in proc.Params.Values) { param = new SqlParameter { ParameterName = item.Name, Value = item.Value, SqlDbType = SettingsHelperManager.DetermineSqlDbTYpe(item.Type), Direction = SettingsHelperManager.GetParametrDirection(item.Direction) }; cmd.Parameters.Add(param); } using (_reader = cmd.ExecuteReader()) { while (_reader.Read()) { container.SetFromReader(ref _reader); } } //set outputparams values if (proc.HasOutputParam) { foreach (DataParam value in proc.OutputParams.Values) { value.Value = cmd.Parameters[value.Name].Value.ToString(); } } if (cmd.Parameters.Count > 0) { if (cmd.Parameters[proc.GetparamsByDirection(ParamDirection.Return)[0].Name] != null) { result.SetCode(Convert.ToInt32(cmd.Parameters[proc.GetparamsByDirection(ParamDirection.Return)[0].Name].Value)); } } //_conn.Close(); // } end of using //&& datasource.AgentState != AgentState.Disconnected) if (datasource.ConnectionLevel == ConnectionLevel.Single && con != null) //&& con.State != ConnectionState.Closed) { con.Dispose(); } } } catch (Exception exc) { throw exc; } result.StopMeashure(); return(result); }