コード例 #1
0
        private void Child_Update(Order order, SqlConnection connection)
        {
            bool cancel = false;
            OnChildUpdating(order, connection, ref cancel);
            if (cancel) return;

            if(connection.State != ConnectionState.Open) connection.Open();
            const string commandText = "UPDATE [dbo].[LineItem] SET [OrderId] = @p_OrderId, [LineNum] = @p_LineNum, [ItemId] = @p_ItemId, [Quantity] = @p_Quantity, [UnitPrice] = @p_UnitPrice WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum; SELECT [OrderId], [LineNum] FROM [dbo].[LineItem] WHERE [OrderId] = @p_OriginalOrderId AND [LineNum] = @p_OriginalLineNum";
            using(var command = new SqlCommand(commandText, connection))
            {
                command.Parameters.AddWithValue("@p_OriginalOrderId", order != null ? order.OrderId : this.OriginalOrderId);
                command.Parameters.AddWithValue("@p_OrderId", order != null ? order.OrderId : this.OrderId);
                command.Parameters.AddWithValue("@p_OriginalLineNum", this.OriginalLineNum);
                command.Parameters.AddWithValue("@p_LineNum", this.LineNum);
                command.Parameters.AddWithValue("@p_ItemId", this.ItemId);
                command.Parameters.AddWithValue("@p_Quantity", this.Quantity);
                command.Parameters.AddWithValue("@p_UnitPrice", this.UnitPrice);

                // result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                int result = command.ExecuteNonQuery();
                if (result == 0)
                    throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.");

                // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query.
                if(order != null && order.OrderId != this.OrderId)
                    LoadProperty(_orderIdProperty, order.OrderId);

                // Update non-identity primary key value.
                LoadProperty(_originalOrderIdProperty, this.OrderId);

                // Update non-identity primary key value.
                LoadProperty(_originalLineNumProperty, this.LineNum);
            }
            
            // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). 
            // TODO: Please override OnChildUpdated() and update this child manually.
            // FieldManager.UpdateChildren(this, connection);

            OnChildUpdated();
        }
コード例 #2
0
        private void Child_Insert(Order order, SqlConnection connection)
        {
            bool cancel = false;
            OnChildInserting(order, connection, ref cancel);
            if (cancel) return;

            if(connection.State != ConnectionState.Open) connection.Open();
            const string commandText = "INSERT INTO [dbo].[OrderStatus] ([OrderId], [LineNum], [Timestamp], [Status]) VALUES (@p_OrderId, @p_LineNum, @p_Timestamp, @p_Status)";
            using(var command = new SqlCommand(commandText, connection))
            {
                command.Parameters.AddWithValue("@p_OrderId", order != null ? order.OrderId : this.OrderId);
                command.Parameters.AddWithValue("@p_LineNum", this.LineNum);
                command.Parameters.AddWithValue("@p_Timestamp", this.Timestamp);
                command.Parameters.AddWithValue("@p_Status", this.Status);

                //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. 
                int result = command.ExecuteNonQuery();
                if (result == 0)
                    throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.");

                // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query.
                if(order != null && order.OrderId != this.OrderId)
                    LoadProperty(_orderIdProperty, order.OrderId);

                // Update the original non-identity primary key value.
                LoadProperty(_originalOrderIdProperty, this.OrderId);

                // Update the original non-identity primary key value.
                LoadProperty(_originalLineNumProperty, this.LineNum);
            }
            
            // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). 
            // TODO: Please override OnChildInserted() and insert this child manually.
            // FieldManager.UpdateChildren(this, connection);

            OnChildInserted();
        }