/// <summary>
 /// Add the info of a DOrder to the databse.
 /// </summary>
 /// <param name="dOrder">DOrder to add.</param>
 private void AddDOrder(DOrder dOrder)
 {
     using (var command = context.CreateCommand())
     {
         command.CommandText = $@"Insert into Orders (OrderDate) " +
                               "values (@orderdate) " +
                               "SELECT CAST(scope_identity() AS int);";
         command.AddParameter("orderdate", dOrder.Date.ToString());
         dOrder.Id = (int)command.ExecuteScalar();
     }
 }
        /// <summary>
        /// Add the info of a DDelivery to the databse.
        /// </summary>
        /// <param name="dDelivery">DDelivery to add.</param>
        private void AddDDelivery(DDelivery dDelivery)
        {
            using (var command = context.CreateCommand())
            {
                command.CommandText = $@"Insert into Deliveries (DeliveryDate, Date) " +
                                      "values (@deliveryDate ,@date) " +
                                      "SELECT CAST(scope_identity() AS int);";

                command.AddParameter("date", dDelivery.Date.ToString());
                command.AddParameter("deliveryDate", dDelivery.DeliveryDate.ToString());
                dDelivery.Id = (int)command.ExecuteScalar();
            }
        }
Esempio n. 3
0
        public void CreateCommand()
        {
            var factory        = Substitute.For <IConnectionFactory>();
            var connectionMock = Substitute.For <IDbConnection>();
            var expected       = Substitute.For <IDbCommand>();

            connectionMock.CreateCommand().Returns(expected);
            factory.Create().Returns(connectionMock);

            var context = new AdoNetContext(factory);
            var command = context.CreateCommand();

            Assert.NotNull(command);
            Assert.Same(expected, command);
        }
Esempio n. 4
0
        public void CommandWithoutTransaction()
        {
            var factory        = Substitute.For <IConnectionFactory>();
            var connectionMock = Substitute.For <IDbConnection>();
            var command        = Substitute.For <IDbCommand>();

            connectionMock.CreateCommand().Returns(command);
            var transaction = Substitute.For <IDbTransaction>();

            connectionMock.BeginTransaction().Returns(transaction);
            factory.Create().Returns(connectionMock);

            var context = new AdoNetContext(factory);

            context.CreateUnitOfWork().Dispose();
            var cmd = context.CreateCommand();

            Assert.NotSame(transaction, cmd.Transaction);
        }
        /// <summary>
        ///  Adds a DAuthor to the database.
        /// </summary>
        /// <param name="dAuthor">author to add</param>
        private void AddDAuthor(DAuthor dAuthor)
        {
            using (var command = context.CreateCommand())
            {
                command.CommandText = @$ "Select * From Authors Where Authors.name = @name";
                command.AddParameter($"name", dAuthor.Name);
                int?id = (int?)command.ExecuteScalar();

                if (id != null)
                {
                    dAuthor.Id = (int)id;
                }
                else
                {
                    command.CommandText = @"Insert into Authors (Name) " +
                                          $"values (@name)" +
                                          "SELECT CAST(scope_identity() AS int);";
                    dAuthor.Id = (int)command.ExecuteScalar();
                }
            }
        }