/// <summary> /// Inserts a PetShop.Business.Category object into the datasource using a transaction. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Category object to insert.</param> /// <remarks> /// After inserting into the datasource, the PetShop.Business.Category 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.Category entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Category_Insert", _useStoredProcedure); database.AddInParameter(commandWrapper, "@CategoryId", DbType.AnsiString, entity.CategoryId); database.AddInParameter(commandWrapper, "@Name", DbType.AnsiString, entity.Name); database.AddInParameter(commandWrapper, "@Descn", DbType.AnsiString, entity.Descn); 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.OriginalCategoryId = entity.CategoryId; 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(OriginalCategoryId != CategoryId) { // Insert new child. Category item = new Category {CategoryId = CategoryId, Name = Name, Description = Description}; item = item.Save(); // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships. foreach(Product itemToUpdate in Products) { itemToUpdate.CategoryId = CategoryId; } // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new CategoryCriteria {CategoryId = OriginalCategoryId}; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalCategoryId = CategoryId; OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Category] SET [CategoryId] = @p_CategoryId, [Name] = @p_Name, [Descn] = @p_Descn WHERE [CategoryId] = @p_OriginalCategoryId; SELECT [CategoryId] FROM [dbo].[Category] WHERE [CategoryId] = @p_OriginalCategoryId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalCategoryId", this.OriginalCategoryId); 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)); //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(_originalCategoryIdProperty, this.CategoryId); } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }
private void Child_Update(Category category, SqlConnection connection) { bool cancel = false; OnChildUpdating(category, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); 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 command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalProductId", this.OriginalProductId); command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_CategoryId", category != null ? category.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."); // 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(category != null && category.CategoryId != this.CategoryId) LoadProperty(_categoryIdProperty, category.CategoryId); // Update non-identity primary key value. LoadProperty(_originalProductIdProperty, this.ProductId); } FieldManager.UpdateChildren(this, null, connection); OnChildUpdated(); }
private void Child_Insert(Category category, SqlConnection connection) { bool cancel = false; OnChildInserting(category, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "INSERT INTO [dbo].[Product] ([ProductId], [CategoryId], [Name], [Descn], [Image]) VALUES (@p_ProductId, @p_CategoryId, @p_Name, @p_Descn, @p_Image)"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_CategoryId", category != null ? category.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."); // 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(category != null && category.CategoryId != this.CategoryId) LoadProperty(_categoryIdProperty, category.CategoryId); // Update the original non-identity primary key value. LoadProperty(_originalProductIdProperty, this.ProductId); } // 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, null, connection); OnChildInserted(); }