/// <summary> /// Inserts a PetShop.Business.Product object into the datasource using a transaction. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Product object to insert.</param> /// <remarks> /// After inserting into the datasource, the PetShop.Business.Product object will be updated /// to refelect any changes made by the datasource. (ie: identity or computed columns) /// </remarks> /// <returns>Returns true if operation is successful.</returns> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override bool Insert(TransactionManager transactionManager, PetShop.Business.Product entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Product_Insert", _useStoredProcedure); database.AddInParameter(commandWrapper, "@ProductId", DbType.AnsiString, entity.ProductId); database.AddInParameter(commandWrapper, "@CategoryId", DbType.AnsiString, entity.CategoryId); database.AddInParameter(commandWrapper, "@Name", DbType.AnsiString, entity.Name); database.AddInParameter(commandWrapper, "@Descn", DbType.AnsiString, entity.Descn); database.AddInParameter(commandWrapper, "@Image", DbType.AnsiString, entity.Image); int results = 0; //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Insert", entity)); if (transactionManager != null) { results = Utility.ExecuteNonQuery(transactionManager, commandWrapper); } else { results = Utility.ExecuteNonQuery(database, commandWrapper); } entity.OriginalProductId = entity.ProductId; entity.AcceptChanges(); //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Insert", entity)); return(Convert.ToBoolean(results)); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) return; if(OriginalProductId != ProductId) { // Insert new child. Product item = new Product {ProductId = ProductId, CategoryId = CategoryId, Name = Name, Description = Description, Image = Image}; item = item.Save(); // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships. foreach(Item itemToUpdate in Items) { itemToUpdate.ProductId = ProductId; } // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new ProductCriteria {ProductId = OriginalProductId}; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalProductId = ProductId; OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Product] SET [ProductId] = @p_ProductId, [CategoryId] = @p_CategoryId, [Name] = @p_Name, [Descn] = @p_Descn, [Image] = @p_Image WHERE [ProductId] = @p_OriginalProductId; SELECT [ProductId] FROM [dbo].[Product] WHERE [ProductId] = @p_OriginalProductId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalProductId", this.OriginalProductId); command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description)); command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image)); //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."); LoadProperty(_originalProductIdProperty, this.ProductId); } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }
private void Child_Update(Product product, Supplier supplier, SqlConnection connection) { bool cancel = false; OnChildUpdating(product, supplier, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "UPDATE [dbo].[Item] SET [ItemId] = @p_ItemId, [ProductId] = @p_ProductId, [ListPrice] = @p_ListPrice, [UnitCost] = @p_UnitCost, [Supplier] = @p_Supplier, [Status] = @p_Status, [Name] = @p_Name, [Image] = @p_Image WHERE [ItemId] = @p_OriginalItemId; SELECT [ItemId] FROM [dbo].[Item] WHERE [ItemId] = @p_OriginalItemId"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalItemId", this.OriginalItemId); command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_ProductId", product != null ? product.ProductId : this.ProductId); command.Parameters.AddWithValue("@p_ListPrice", ADOHelper.NullCheck(this.ListPrice)); command.Parameters.AddWithValue("@p_UnitCost", ADOHelper.NullCheck(this.UnitCost)); command.Parameters.AddWithValue("@p_Supplier", ADOHelper.NullCheck(supplier != null ? supplier.SuppId : this.Supplier)); command.Parameters.AddWithValue("@p_Status", ADOHelper.NullCheck(this.Status)); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image)); // 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(product != null && product.ProductId != this.ProductId) LoadProperty(_productIdProperty, product.ProductId); // 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(supplier != null && supplier.SuppId != this.Supplier) LoadProperty(_supplierProperty, supplier.SuppId); // Update non-identity primary key value. LoadProperty(_originalItemIdProperty, this.ItemId); } // 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(); }
private void Child_Update(Product product, SqlConnection connection) { Child_Update(product, null, connection); }
private void Child_Insert(Product product, SqlConnection connection) { Child_Insert(product, null, connection); }