Пример #1
0
        public void GetCustOrdersDetailTest()
        {
            var result = new CustOrdersDetail();

            result.Discount      = new List <int>();
            result.ExtendedPrice = new List <decimal>();
            result.ProductName   = new List <string>();
            result.Quantity      = new List <short>();
            result.UnitPrice     = new List <decimal>();

            var resExecMethod = new CustOrdersDetail();

            using (DbConnection connection = this.providerFactory.CreateConnection())
            {
                connection.ConnectionString = this.connectionString;
                connection.Open();

                var command = (SqlCommand)connection.CreateCommand();

                //  SELECT TOP 1 OrderID FROM [dbo].[Orders] - вернул OrderID записи = 11079, у которого все поля дат null :
                //  11079;DUMON;5;NULL;NULL;NULL;NULL;0,00;Du monde entier;67, rue des Cinquante Otages;Nantes;NULL;44000;France
                //  Добавление условия выборки "WHERE OrderDate IS NOT NULL" исправило ситуацию
                command.CommandText = "SELECT TOP 1 OrderID FROM [dbo].[Orders] WHERE OrderDate IS NOT NULL";
                result.OrderId      = (int)command.ExecuteScalar();
                resExecMethod       = this.repository.GetCustOrdersDetail(result.OrderId);

                command.Parameters.AddWithValue("@orderId", result.OrderId);
                command.CommandText = "EXECUTE [dbo].[CustOrdersDetail] @OrderID = @orderId";

                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result.ProductName.Add((string)reader["ProductName"]);
                        result.UnitPrice.Add((decimal)reader["UnitPrice"]);
                        result.Quantity.Add((short)reader["Quantity"]);
                        result.Discount.Add((int)reader["Discount"]);
                        result.ExtendedPrice.Add((decimal)reader["ExtendedPrice"]);
                    }
                }
            }

            Assert.AreEqual(resExecMethod.OrderId, result.OrderId);
            Assert.AreEqual(resExecMethod.ProductName[0], result.ProductName[0]);
            Assert.AreEqual(resExecMethod.Quantity[0], result.Quantity[0]);
            Assert.AreEqual(resExecMethod.UnitPrice[0], result.UnitPrice[0]);
            Assert.AreEqual(resExecMethod.ExtendedPrice[0], result.ExtendedPrice[0]);
        }
        /// <inheritdoc/>
        public virtual IEnumerable <CustOrdersDetail> GetCustOrdersDetail(int orderId)
        {
            using (var connection = _providerFactory.CreateConnection())
            {
                connection.ConnectionString = _connectionString;
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "CustOrdersDetail";
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(
                        new SqlParameter
                    {
                        ParameterName = "@OrderID",
                        Value         = orderId,
                        DbType        = DbType.Int32
                    }
                        );

                    using (var reader = command.ExecuteReader())
                    {
                        var custList = new List <CustOrdersDetail>();

                        while (reader.Read())
                        {
                            var custOrdersDetail = new CustOrdersDetail();

                            custOrdersDetail.ProductName   = (string)reader["ProductName"];
                            custOrdersDetail.UnitPrice     = (decimal)reader["UnitPrice"];
                            custOrdersDetail.Quantity      = (short)reader["Quantity"];
                            custOrdersDetail.Discount      = (int)reader["Discount"];
                            custOrdersDetail.ExtendedPrice = (decimal)reader["ExtendedPrice"];

                            custList.Add(custOrdersDetail);
                        }

                        return(custList);
                    }
                }
            }
        }