Пример #1
0
        private void Connect_Read_Template(string strConnection)
        {
            // This method uses a template method to create a Data Access Layer (DAL) to the database
            Console.Write("(template)");
            if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataReader)
            {
                // Using System.Data.Odbc.OdbcDataReader : IDataReader
                Console.WriteLine(" (Odbc.OdbcDataReader, strongly typed)");
                Collection <Northwind_Products> products = null;
                using (NorthwindReader_Products reader = new NorthwindReader_Products())
                {
                    reader.DbTechnology     = m_cfgDatabase.dbTech;
                    reader.ConnectionString = strConnection;
                    reader.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                    reader.RecordsToRead    = (
                        Northwind_Products.colToReadProductID +
                        Northwind_Products.colToReadUnitPrice +
                        Northwind_Products.colToReadProductName);
                    products = reader.Execute();
                }

                Connect_Read_Template_Typed(ref products);
            }
            else if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataAdapter)
            {
                // Using System.Data.Odbc.OdbcDataAdapter : IDbDataAdapter
                Console.WriteLine(" (Odbc.OdbcDataAdapter, strongly typed)");
                Collection <Northwind_Products> products = null;
                using (NorthwindAdapter_Products adapter = new NorthwindAdapter_Products())
                {
                    adapter.DbTechnology     = m_cfgDatabase.dbTech;
                    adapter.ConnectionString = strConnection;
                    adapter.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                    adapter.RecordsToRead    = (
                        Northwind_Products.colToReadProductID +
                        Northwind_Products.colToReadUnitPrice +
                        Northwind_Products.colToReadProductName);
                    products = adapter.Execute();
                }
                Connect_Read_Template_Typed(ref products);
            }
            else if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataSet)
            {
                // Using System.Data.Odbc.OdbcDataAdapter : IDbDataAdapter
                Console.WriteLine(" (Odbc.OdbcDataAdapter, raw DataSet)");
                DataSet          products = null;
                ObjectDataSetRaw adapter  = new ObjectDataSetRaw();
                adapter.DbTechnology     = m_cfgDatabase.dbTech;
                adapter.ConnectionString = strConnection;
                adapter.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                products = adapter.Execute();
                Connect_Read_Template_Raw(ref products);
            }

            Console.WriteLine();
        }
Пример #2
0
        protected override int Connect_PerformanceTest(string strConnection)
        {
            // Version for performance testing
            int recordsRead = 0;

            // This version uses System.Data.OleDb.OleDbDataReader
            using (OleDbConnection connection = new OleDbConnection(strConnection))
            {
                Collection <Northwind_Products> products = null;
                if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataReader)
                {
                    // Using System.Data.OleDb.OleDbDataReader : IDataReader
                    using (NorthwindReader_Products reader = new NorthwindReader_Products())
                    {
                        reader.DbTechnology     = m_cfgDatabase.dbTech;
                        reader.ConnectionString = strConnection;
                        reader.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                        products = reader.Execute();
                    }
                }
                else if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataAdapter)
                {
                    // Using System.Data.OleDb.OleDbDataAdapter : IDbDataAdapter
                    using (NorthwindAdapter_Products adapter = new NorthwindAdapter_Products())
                    {
                        adapter.DbTechnology     = m_cfgDatabase.dbTech;
                        adapter.ConnectionString = strConnection;
                        adapter.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                        products = adapter.Execute();
                    }
                }

                recordsRead = products.Count;
            }

            return(recordsRead);
        }
Пример #3
0
        protected override int Connect_PerformanceTest(string strConnection)
        {
            // Version for performance testing
            int recordsRead = 0;

            using (OdbcConnection connection = new OdbcConnection(strConnection))
            {
                // Use template methods
                Collection <Northwind_Products> products = null;
                if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataReader)
                {
                    // Using System.Data.Odbc.OdbcDataReader : IDataReader
                    using (NorthwindReader_Products reader = new NorthwindReader_Products())
                    {
                        reader.DbTechnology     = m_cfgDatabase.dbTech;
                        reader.ConnectionString = strConnection;
                        reader.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                        products = reader.Execute();
                    }
                }
                else if (m_eDbReadTechnology == DatabaseReadTechnology.eRbRead_DataAdapter)
                {
                    // Using System.Data.Odbc.OdbcDataAdapter : IDbDataAdapter
                    using (NorthwindAdapter_Products adapter = new NorthwindAdapter_Products())
                    {
                        adapter.DbTechnology     = m_cfgDatabase.dbTech;
                        adapter.ConnectionString = strConnection;
                        adapter.CmdText          = m_cfgDatabase.querySELECT.Replace("?", m_cfgDatabase.paramValue.ToString());
                        products = adapter.Execute();
                    }
                }

                recordsRead = products.Count;

                // Use raw DbDataReader

                /* // Create the Command and Parameter objects
                 * OdbcCommand command = new OdbcCommand(m_cfgDatabase.querySELECT, connection);
                 * command.Parameters.AddWithValue("@pricePoint", m_cfgDatabase.paramValue);
                 *
                 * // Open the connection in a try/catch block
                 * try
                 * {
                 *  // Create and execute the DataReader; for this performance version just count
                 *  // the number of records read
                 *  connection.Open();
                 *  OdbcDataReader reader = command.ExecuteReader();
                 *  while (reader.Read())
                 *  {
                 *      recordsRead++;
                 *  }
                 *  reader.Close();
                 * }
                 * catch (Exception ex)
                 * {
                 *  Console.WriteLine(UtilitiesGeneral.FormatException(
                 *      this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
                 * }*/
            }

            return(recordsRead);
        }