示例#1
0
        public static List <Invoice> GetInvoices()
        {
            List <Invoice> invoices = new List <Invoice>();

            SqlConnection connection = MMABooksDB.GetConnection();
            string        selectStatement
                = "SELECT * FROM Invoices";
            SqlCommand selectCommand =
                new SqlCommand(selectStatement, connection);

            try
            {
                connection.Open();
                SqlDataReader invoiceReader =
                    selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                while (invoiceReader.Read())
                {
                    Invoice invoice = new Invoice();
                    invoice.InvoiceID    = (int)invoiceReader["InvoiceID"];
                    invoice.CustomerID   = (int)invoiceReader["CustomerID"];
                    invoice.InvoiceDate  = (DateTime)invoiceReader["InvoiceDate"];
                    invoice.ProductTotal = (decimal)invoiceReader["ProductTotal"];
                    invoice.SalesTax     = (decimal)invoiceReader["SalesTax"];
                    invoice.Shipping     = (decimal)invoiceReader["Shipping"];
                    invoice.InvoiceTotal = (decimal)invoiceReader["InvoiceTotal"];
                    invoices.Add(invoice);
                }
                invoiceReader.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
            return(invoices);
        }
        public static List <LineItem> GetLineItems()
        {
            List <LineItem> lineItems = new List <LineItem>();

            SqlConnection connection = MMABooksDB.GetConnection();
            string        selectStatement
                = "SELECT * FROM InvoiceLineItems";
            SqlCommand selectCommand =
                new SqlCommand(selectStatement, connection);

            try
            {
                connection.Open();
                SqlDataReader lineItemReader =
                    selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                while (lineItemReader.Read())
                {
                    LineItem lineItem = new LineItem();
                    lineItem.InvoiceID   = (int)lineItemReader["InvoiceID"];
                    lineItem.ProductCode = lineItemReader["ProductCode"].ToString();
                    lineItem.UnitPrice   = (decimal)lineItemReader["UnitPrice"];
                    lineItem.Quantity    = (int)lineItemReader["Quantity"];
                    lineItem.ItemTotal   = (decimal)lineItemReader["ItemTotal"];
                    lineItems.Add(lineItem);
                }
                lineItemReader.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
            return(lineItems);
        }