sheryever
4/6/2017 - 7:56 AM

OralceConnection & DatasetExtension to execute queries and get data

OralceConnection & DatasetExtension to execute queries and get data

    public static class DatasetExtension
    {    
        public static T GetAs<T>(this DataSet dataSet, Action<T, DataRow> mapAction)
            where T : class, new()
        {
            
            if (dataSet == null || dataSet.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            var result = new T();

            mapAction.Invoke(result, dataSet.Tables[0].Rows[0]);


            return result;
        }

        public static IEnumerable<T> GetAllAs<T>(this DataSet dataSet, Action<T, DataRow> mapAction)
            where T : class, new()
        {
            var result = new List<T>();
            if (dataSet == null || dataSet.Tables[0].Rows.Count < 1)
            {
                return result;
            }

            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                var entity = new T();
                mapAction.Invoke(entity, row);
                result.Add(entity);
            }

            return result;
        }

//      calling GetAs
//      using (var connection = new OracleConnection(OracleConnections.MadDis))
//      {
//          var result = connection.GetAll<Person>(query, (person, dataRow) => { 
//             person.Id = dataRow["ID"];
//             person.Name = dataRow["Name"];
//             person.Address = dataRow["Address"];
//          });
//          return result;
//       }
        public static T GetAs<T>(this OracleConnection connection, string query, Action<T, DataRow> mapAction)
            where T : class, new()
        {

            var dataSet = connection.ExecuteQuery(query);


            if (dataSet == null || dataSet.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            var result = new T();
            mapAction.Invoke(result, dataSet.Tables[0].Rows[0]);
            

            return result;
        }

        public static IEnumerable<T> GetAllAs<T>(this OracleConnection connection, string query, Action<T, DataRow> mapAction)
            where T : class, new()
        {

            var dataSet = connection.ExecuteQuery(query);

            var result = new List<T>();
            if (dataSet == null || dataSet.Tables[0].Rows.Count < 1)
            {
                return result;
            }

            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                var entity = new T();
                mapAction.Invoke(entity, row);
                result.Add(entity);
            }

            return result;
        }

        public static DataSet ExecuteQuery(this OracleConnection connection, string query)
        {
            var dataset = new DataSet();

            using (var dataAdapter = new OracleDataAdapter(query, connection))
            {
                dataAdapter.Fill(dataset);
            }

            return dataset;
        }
    }