Beispiel #1
0
        // Return an enumerable collection of pocos
        public IEnumerable <object> Query(PocoData pd, string sql, params object[] args)
        {
            if (EnableAutoSelect)
            {
                sql = AddSelectClause(sql, pd);
            }

            OpenSharedConnection();
            try
            {
                using (var cmd = CreateCommand(this.Connection, sql, args))
                {
                    IDataReader r;
                    try
                    {
                        r = cmd.ExecuteReader();
                        OnExecutedCommand(cmd);
                    }
                    catch (Exception x)
                    {
                        OnException(x);
                        throw;
                    }
                    var factory = pd.GetFactory(cmd.CommandText, Connection.ConnectionString, ForceDateTimesToUtc, 0, r.FieldCount, r) as Func <IDataReader, object>;
                    using (r)
                    {
                        while (true)
                        {
                            object poco = Activator.CreateInstance(pd.type);
                            try
                            {
                                if (!r.Read())
                                {
                                    yield break;
                                }
                                poco = factory(r);
                            }
                            catch (Exception x)
                            {
                                OnException(x);
                                throw;
                            }

                            yield return(poco);
                        }
                    }
                }
            }
            finally
            {
                CloseSharedConnection();
            }
        }