Esempio n. 1
0
        public void Select_MultipleResultSets()
        {
            var batchCmdBuilder = new DbBatchCommandBuilder(DbAdapter.CommandBuilder.DbFactory);

            batchCmdBuilder.BeginBatch();
            batchCmdBuilder.GetSelectCommand(new Query("companies"));
            batchCmdBuilder.GetSelectCommand(new Query("contacts"));
            var selectMultipleCmd = batchCmdBuilder.EndBatch();

            (var companies, var contacts) = DbAdapter.Select(selectMultipleCmd).ExecuteReader((rdr) => {
                var companiesRes = new DataReaderResult(rdr).ToList <CompanyModelAnnotated>();
                rdr.NextResult();
                var contactsRes = new DataReaderResult(rdr).ToList <ContactModel>();
                return(companiesRes, contactsRes);
            });
            Assert.Equal(2, companies.Count);
            Assert.Equal(5, contacts.Count);
        }
Esempio n. 2
0
        public static void RunSelectMultipleResultSet(DbContext dbContext)
        {
            // lets generate several selects in one command
            var batchCmdBuilder = new DbBatchCommandBuilder(dbContext.DbFactory);

            Console.WriteLine("Composing 2 selects in one DbCommand (multiple result sets)");
            batchCmdBuilder.BeginBatch();

            batchCmdBuilder.GetSelectCommand(new Query("Customers", (QField)"Country" == (QConst)"Germany"));
            batchCmdBuilder.GetSelectCommand(new Query("Orders",
                                                       new QConditionNode((QField)"CustomerID", Conditions.In,
                                                                          new Query("Customers.c", (QField)"c.Country" == (QConst)"Germany").Select("c.CustomerID")
                                                                          )
                                                       ));

            var multiSelectCmd = batchCmdBuilder.EndBatch();

            multiSelectCmd.Connection = dbContext.Connection;

            RecordSet customerRS = null;
            RecordSet orderRS    = null;

            dbContext.Connection.Open();
            try {
                using (var rdr = multiSelectCmd.ExecuteReader()) {
                    customerRS = RecordSet.FromReader(rdr);
                    if (rdr.NextResult())
                    {
                        orderRS = RecordSet.FromReader(rdr);
                    }
                }
            } finally {
                dbContext.Connection.Close();
            }

            Console.WriteLine($"Loaded {customerRS.Count} customers and {orderRS.Count} their orders from one data reader");
        }