private void Child_Update(Profile profile, SqlConnection connection) { bool cancel = false; OnChildUpdating(profile, connection, ref cancel); if (cancel) { return; } if (connection.State != ConnectionState.Open) { connection.Open(); } const string commandText = "UPDATE [dbo].[Account] SET [UniqueID] = @p_UniqueID, [Email] = @p_Email, [FirstName] = @p_FirstName, [LastName] = @p_LastName, [Address1] = @p_Address1, [Address2] = @p_Address2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Country] = @p_Country, [Phone] = @p_Phone WHERE [AccountId] = @p_AccountId"; using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_AccountId", this.AccountId); command.Parameters.AddWithValue("@p_UniqueID", profile != null ? profile.UniqueID : this.UniqueID); command.Parameters.AddWithValue("@p_Email", this.Email); command.Parameters.AddWithValue("@p_FirstName", this.FirstName); command.Parameters.AddWithValue("@p_LastName", this.LastName); command.Parameters.AddWithValue("@p_Address1", this.Address1); command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(this.Address2)); command.Parameters.AddWithValue("@p_City", this.City); command.Parameters.AddWithValue("@p_State", this.State); command.Parameters.AddWithValue("@p_Zip", this.Zip); command.Parameters.AddWithValue("@p_Country", this.Country); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { } } // 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 (profile != null && profile.UniqueID != this.UniqueID) { LoadProperty(_uniqueIDProperty, profile.UniqueID); } } // 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(); }
protected override void DataPortal_Execute() { string commandText = String.Format("SELECT COUNT(1) FROM {0} {1}", Criteria.TableFullName, ADOHelper.BuildWhereStatement(Criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(Criteria.StateBag)); Result = (int)command.ExecuteScalar() > 0; } } }
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(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) { return; } const string commandText = "UPDATE [dbo].[Profiles] SET [Username] = @p_Username, [ApplicationName] = @p_ApplicationName, [IsAnonymous] = @p_IsAnonymous, [LastActivityDate] = @p_LastActivityDate, [LastUpdatedDate] = @p_LastUpdatedDate WHERE [UniqueID] = @p_UniqueID"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", this.UniqueID); command.Parameters.AddWithValue("@p_Username", this.Username); command.Parameters.AddWithValue("@p_ApplicationName", this.ApplicationName); command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(this.IsAnonymous)); command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(this.LastActivityDate)); command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(this.LastUpdatedDate)); using (var reader = new SafeDataReader(command.ExecuteReader())) { //RecordsAffected: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. if (reader.RecordsAffected == 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."); } if (reader.Read()) { using (BypassPropertyChecks) { } } } } 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(); }
/// <summary> /// Inserts data into the data base using the information in the current /// CSLA editable child business object of type <see cref="Account"/> /// </summary> /// <returns></returns> private void Child_Insert(SqlConnection connection) { bool cancel = false; OnChildInserting(connection, ref cancel); if (cancel) { return; } if (connection.State != ConnectionState.Open) { connection.Open(); } const string commandText = "INSERT INTO [dbo].[Account] ([UniqueID], [Email], [FirstName], [LastName], [Address1], [Address2], [City], [State], [Zip], [Country], [Phone]) VALUES (@p_UniqueID, @p_Email, @p_FirstName, @p_LastName, @p_Address1, @p_Address2, @p_City, @p_State, @p_Zip, @p_Country, @p_Phone); SELECT [AccountId] FROM [dbo].[Account] WHERE AccountId = SCOPE_IDENTITY()"; using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", this.UniqueID); command.Parameters.AddWithValue("@p_Email", this.Email); command.Parameters.AddWithValue("@p_FirstName", this.FirstName); command.Parameters.AddWithValue("@p_LastName", this.LastName); command.Parameters.AddWithValue("@p_Address1", this.Address1); command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(this.Address2)); command.Parameters.AddWithValue("@p_City", this.City); command.Parameters.AddWithValue("@p_State", this.State); command.Parameters.AddWithValue("@p_Zip", this.Zip); command.Parameters.AddWithValue("@p_Country", this.Country); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { // Update identity primary key value. LoadProperty(_accountIdProperty, reader.GetInt32("AccountId")); } } } FieldManager.UpdateChildren(this, connection); OnChildInserted(); }
private void Child_Update(SqlConnection connection) { bool cancel = false; OnChildUpdating(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", 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(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 non-identity primary key value. LoadProperty(_originalItemIdProperty, this.ItemId); } FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
/// <summary> /// Updates the corresponding record in the data base with the information in the current /// CSLA editable child business object of type <see cref="Account"/> /// </summary> /// <returns></returns> private void Child_Update(SqlConnection connection) { bool cancel = false; OnChildUpdating(connection, ref cancel); if (cancel) { return; } if (connection.State != ConnectionState.Open) { connection.Open(); } const string commandText = "UPDATE [dbo].[Account] SET [UniqueID] = @p_UniqueID, [Email] = @p_Email, [FirstName] = @p_FirstName, [LastName] = @p_LastName, [Address1] = @p_Address1, [Address2] = @p_Address2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Country] = @p_Country, [Phone] = @p_Phone WHERE [AccountId] = @p_AccountId"; using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_AccountId", this.AccountId); command.Parameters.AddWithValue("@p_UniqueID", this.UniqueID); command.Parameters.AddWithValue("@p_Email", this.Email); command.Parameters.AddWithValue("@p_FirstName", this.FirstName); command.Parameters.AddWithValue("@p_LastName", this.LastName); command.Parameters.AddWithValue("@p_Address1", this.Address1); command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(this.Address2)); command.Parameters.AddWithValue("@p_City", this.City); command.Parameters.AddWithValue("@p_State", this.State); command.Parameters.AddWithValue("@p_Zip", this.Zip); command.Parameters.AddWithValue("@p_Country", this.Country); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { } } } FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
protected override void DataPortal_Insert() { bool cancel = false; OnInserting(ref cancel); if (cancel) { return; } const string commandText = "INSERT INTO [dbo].[Supplier] ([SuppId], [Name], [Status], [Addr1], [Addr2], [City], [State], [Zip], [Phone]) VALUES (@p_SuppId, @p_Name, @p_Status, @p_Addr1, @p_Addr2, @p_City, @p_State, @p_Zip, @p_Phone)"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_SuppId", this.SuppId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Status", this.Status); command.Parameters.AddWithValue("@p_Addr1", ADOHelper.NullCheck(this.Addr1)); command.Parameters.AddWithValue("@p_Addr2", ADOHelper.NullCheck(this.Addr2)); command.Parameters.AddWithValue("@p_City", ADOHelper.NullCheck(this.City)); command.Parameters.AddWithValue("@p_State", ADOHelper.NullCheck(this.State)); command.Parameters.AddWithValue("@p_Zip", ADOHelper.NullCheck(this.Zip)); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); //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(_originalSuppIdProperty, this.SuppId); } FieldManager.UpdateChildren(this, connection); } OnInserted(); }
protected override void DataPortal_Insert() { bool cancel = false; OnInserting(ref cancel); if (cancel) { return; } const string commandText = "INSERT INTO [dbo].[Item] ([ItemId], [ProductId], [ListPrice], [UnitCost], [Supplier], [Status], [Name], [Image]) VALUES (@p_ItemId, @p_ProductId, @p_ListPrice, @p_UnitCost, @p_Supplier, @p_Status, @p_Name, @p_Image)"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_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(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."); } LoadProperty(_originalItemIdProperty, this.ItemId); } FieldManager.UpdateChildren(this, connection); } OnInserted(); }
protected override void DataPortal_Insert() { bool cancel = false; OnInserting(ref cancel); if (cancel) { return; } const string commandText = "INSERT INTO [dbo].[Profiles] ([Username], [ApplicationName], [IsAnonymous], [LastActivityDate], [LastUpdatedDate]) VALUES (@p_Username, @p_ApplicationName, @p_IsAnonymous, @p_LastActivityDate, @p_LastUpdatedDate); SELECT [UniqueID] FROM [dbo].[Profiles] WHERE UniqueID = SCOPE_IDENTITY()"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_Username", this.Username); command.Parameters.AddWithValue("@p_ApplicationName", this.ApplicationName); command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(this.IsAnonymous)); command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(this.LastActivityDate)); command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(this.LastUpdatedDate)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { using (BypassPropertyChecks) { LoadProperty(_uniqueIDProperty, reader.GetInt32("UniqueID")); } } } } FieldManager.UpdateChildren(this, connection); } OnInserted(); }
private void Child_Fetch(CartCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } RaiseListChangedEvents = false; // Fetch Child objects. string commandText = String.Format("SELECT [CartId], [UniqueID], [ItemId], [Name], [Type], [Price], [CategoryId], [ProductId], [IsShoppingCart], [Quantity] FROM [dbo].[Cart] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { do { this.Add(PetShop.Business.Cart.GetCart(reader)); } while(reader.Read()); } } } } RaiseListChangedEvents = true; OnFetched(); }
private void Child_Insert(SqlConnection connection) { bool cancel = false; OnChildInserting(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", 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 the original non-identity primary key value. LoadProperty(_originalProductIdProperty, this.ProductId); } FieldManager.UpdateChildren(this, connection); OnChildInserted(); }
private void Child_Fetch(OrderCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } RaiseListChangedEvents = false; // Fetch Child objects. string commandText = String.Format("SELECT [OrderId], [UserId], [OrderDate], [ShipAddr1], [ShipAddr2], [ShipCity], [ShipState], [ShipZip], [ShipCountry], [BillAddr1], [BillAddr2], [BillCity], [BillState], [BillZip], [BillCountry], [Courier], [TotalPrice], [BillToFirstName], [BillToLastName], [ShipToFirstName], [ShipToLastName], [AuthorizationNumber], [Locale] FROM [dbo].[Orders] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { do { this.Add(PetShop.Business.Order.GetOrder(reader)); } while(reader.Read()); } } } } RaiseListChangedEvents = true; OnFetched(); }
private void DataPortal_Fetch(ProfileCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } RaiseListChangedEvents = false; // Fetch Child objects. string commandText = String.Format("SELECT [UniqueID], [Username], [ApplicationName], [IsAnonymous], [LastActivityDate], [LastUpdatedDate] FROM [dbo].[Profiles] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { do { this.Add(PetShop.Business.Profile.GetProfile(reader)); } while(reader.Read()); } } } } RaiseListChangedEvents = true; OnFetched(); }
private void Child_Fetch(SupplierCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } RaiseListChangedEvents = false; // Fetch Child objects. string commandText = String.Format("SELECT [SuppId], [Name], [Status], [Addr1], [Addr2], [City], [State], [Zip], [Phone] FROM [dbo].[Supplier] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { do { this.Add(PetShop.Business.Supplier.GetSupplier(reader)); } while(reader.Read()); } } } } RaiseListChangedEvents = true; OnFetched(); }
private void DataPortal_Fetch(LineItemCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } RaiseListChangedEvents = false; // Fetch Child objects. string commandText = String.Format("SELECT [OrderId], [LineNum], [ItemId], [Quantity], [UnitPrice] FROM [dbo].[LineItem] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { do { this.Add(PetShop.Business.LineItem.GetLineItem(reader)); } while(reader.Read()); } } } } RaiseListChangedEvents = true; OnFetched(); }
private void Child_Insert(SqlConnection connection) { bool cancel = false; OnChildInserting(connection, ref cancel); if (cancel) { return; } if (connection.State != ConnectionState.Open) { connection.Open(); } const string commandText = "INSERT INTO [dbo].[Profiles] ([Username], [ApplicationName], [IsAnonymous], [LastActivityDate], [LastUpdatedDate]) VALUES (@p_Username, @p_ApplicationName, @p_IsAnonymous, @p_LastActivityDate, @p_LastUpdatedDate); SELECT [UniqueID] FROM [dbo].[Profiles] WHERE UniqueID = SCOPE_IDENTITY()"; using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_Username", this.Username); command.Parameters.AddWithValue("@p_ApplicationName", this.ApplicationName); command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(this.IsAnonymous)); command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(this.LastActivityDate)); command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(this.LastUpdatedDate)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { // Update identity primary key value. LoadProperty(_uniqueIDProperty, reader.GetInt32("UniqueID")); } } } FieldManager.UpdateChildren(this, connection); OnChildInserted(); }
private void Child_Fetch(OrderCriteria criteria) { bool cancel = false; OnChildFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [OrderId], [UserId], [OrderDate], [ShipAddr1], [ShipAddr2], [ShipCity], [ShipState], [ShipZip], [ShipCountry], [BillAddr1], [BillAddr2], [BillCity], [BillState], [BillZip], [BillCountry], [Courier], [TotalPrice], [BillToFirstName], [BillToLastName], [ShipToFirstName], [ShipToLastName], [AuthorizationNumber], [Locale] FROM [dbo].[Orders] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Orders' using the following criteria: {0}.", criteria)); } } } } OnChildFetched(); MarkAsChild(); }
private void Child_Fetch(LineItemCriteria criteria) { bool cancel = false; OnChildFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [OrderId], [LineNum], [ItemId], [Quantity], [UnitPrice] FROM [dbo].[LineItem] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.LineItem' using the following criteria: {0}.", criteria)); } } } } OnChildFetched(); MarkAsChild(); }
private void Child_Fetch(ProductCriteria criteria) { bool cancel = false; OnChildFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [ProductId], [CategoryId], [Name], [Descn], [Image] FROM [dbo].[Product] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Product' using the following criteria: {0}.", criteria)); } } } } OnChildFetched(); MarkAsChild(); }
private void Child_Fetch(ProfileCriteria criteria) { bool cancel = false; OnChildFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [UniqueID], [Username], [ApplicationName], [IsAnonymous], [LastActivityDate], [LastUpdatedDate] FROM [dbo].[Profiles] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Profiles' using the following criteria: {0}.", criteria)); } } } } OnChildFetched(); MarkAsChild(); }
private void Child_Update(SqlConnection connection) { bool cancel = false; OnChildUpdating(connection, ref cancel); if (cancel) { return; } if (connection.State != ConnectionState.Open) { connection.Open(); } const string commandText = "UPDATE [dbo].[Profiles] SET [Username] = @p_Username, [ApplicationName] = @p_ApplicationName, [IsAnonymous] = @p_IsAnonymous, [LastActivityDate] = @p_LastActivityDate, [LastUpdatedDate] = @p_LastUpdatedDate WHERE [UniqueID] = @p_UniqueID"; using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", this.UniqueID); command.Parameters.AddWithValue("@p_Username", this.Username); command.Parameters.AddWithValue("@p_ApplicationName", this.ApplicationName); command.Parameters.AddWithValue("@p_IsAnonymous", ADOHelper.NullCheck(this.IsAnonymous)); command.Parameters.AddWithValue("@p_LastActivityDate", ADOHelper.NullCheck(this.LastActivityDate)); command.Parameters.AddWithValue("@p_LastUpdatedDate", ADOHelper.NullCheck(this.LastUpdatedDate)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { } } } FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
private void DataPortal_Fetch(ItemCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [ItemId], [ProductId], [ListPrice], [UnitCost], [Supplier], [Status], [Name], [Image] FROM [dbo].[Item] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Item' using the following criteria: {0}.", criteria)); } } } } OnFetched(); }
protected void DataPortal_Fetch(SupplierCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [SuppId], [Name], [Status], [Addr1], [Addr2], [City], [State], [Zip], [Phone] FROM [dbo].[Supplier] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Supplier' using the following criteria: {0}.", criteria)); } } } } OnFetched(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) { return; } if (OriginalSuppId != SuppId) { // Insert new child. Supplier item = new Supplier { SuppId = SuppId, Name = Name, Status = Status, Addr1 = Addr1, Addr2 = Addr2, City = City, State = State, Zip = Zip, Phone = Phone }; 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.Supplier = SuppId; } // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new SupplierCriteria { SuppId = OriginalSuppId }; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalSuppId = SuppId; OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Supplier] SET [SuppId] = @p_SuppId, [Name] = @p_Name, [Status] = @p_Status, [Addr1] = @p_Addr1, [Addr2] = @p_Addr2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Phone] = @p_Phone WHERE [SuppId] = @p_OriginalSuppId; SELECT [SuppId] FROM [dbo].[Supplier] WHERE [SuppId] = @p_OriginalSuppId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalSuppId", this.OriginalSuppId); command.Parameters.AddWithValue("@p_SuppId", this.SuppId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Status", this.Status); command.Parameters.AddWithValue("@p_Addr1", ADOHelper.NullCheck(this.Addr1)); command.Parameters.AddWithValue("@p_Addr2", ADOHelper.NullCheck(this.Addr2)); command.Parameters.AddWithValue("@p_City", ADOHelper.NullCheck(this.City)); command.Parameters.AddWithValue("@p_State", ADOHelper.NullCheck(this.State)); command.Parameters.AddWithValue("@p_Zip", ADOHelper.NullCheck(this.Zip)); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); //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(_originalSuppIdProperty, this.SuppId); } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) { return; } const string commandText = "UPDATE [dbo].[Orders] SET [UserId] = @p_UserId, [OrderDate] = @p_OrderDate, [ShipAddr1] = @p_ShipAddr1, [ShipAddr2] = @p_ShipAddr2, [ShipCity] = @p_ShipCity, [ShipState] = @p_ShipState, [ShipZip] = @p_ShipZip, [ShipCountry] = @p_ShipCountry, [BillAddr1] = @p_BillAddr1, [BillAddr2] = @p_BillAddr2, [BillCity] = @p_BillCity, [BillState] = @p_BillState, [BillZip] = @p_BillZip, [BillCountry] = @p_BillCountry, [Courier] = @p_Courier, [TotalPrice] = @p_TotalPrice, [BillToFirstName] = @p_BillToFirstName, [BillToLastName] = @p_BillToLastName, [ShipToFirstName] = @p_ShipToFirstName, [ShipToLastName] = @p_ShipToLastName, [AuthorizationNumber] = @p_AuthorizationNumber, [Locale] = @p_Locale WHERE [OrderId] = @p_OrderId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OrderId", this.OrderId); command.Parameters.AddWithValue("@p_UserId", this.UserId); command.Parameters.AddWithValue("@p_OrderDate", this.OrderDate); command.Parameters.AddWithValue("@p_ShipAddr1", this.ShipAddr1); command.Parameters.AddWithValue("@p_ShipAddr2", ADOHelper.NullCheck(this.ShipAddr2)); command.Parameters.AddWithValue("@p_ShipCity", this.ShipCity); command.Parameters.AddWithValue("@p_ShipState", this.ShipState); command.Parameters.AddWithValue("@p_ShipZip", this.ShipZip); command.Parameters.AddWithValue("@p_ShipCountry", this.ShipCountry); command.Parameters.AddWithValue("@p_BillAddr1", this.BillAddr1); command.Parameters.AddWithValue("@p_BillAddr2", ADOHelper.NullCheck(this.BillAddr2)); command.Parameters.AddWithValue("@p_BillCity", this.BillCity); command.Parameters.AddWithValue("@p_BillState", this.BillState); command.Parameters.AddWithValue("@p_BillZip", this.BillZip); command.Parameters.AddWithValue("@p_BillCountry", this.BillCountry); command.Parameters.AddWithValue("@p_Courier", this.Courier); command.Parameters.AddWithValue("@p_TotalPrice", this.TotalPrice); command.Parameters.AddWithValue("@p_BillToFirstName", this.BillToFirstName); command.Parameters.AddWithValue("@p_BillToLastName", this.BillToLastName); command.Parameters.AddWithValue("@p_ShipToFirstName", this.ShipToFirstName); command.Parameters.AddWithValue("@p_ShipToLastName", this.ShipToLastName); command.Parameters.AddWithValue("@p_AuthorizationNumber", this.AuthorizationNumber); command.Parameters.AddWithValue("@p_Locale", this.Locale); using (var reader = new SafeDataReader(command.ExecuteReader())) { //RecordsAffected: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. if (reader.RecordsAffected == 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."); } if (reader.Read()) { using (BypassPropertyChecks) { } } } } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }
/// <summary> /// Retrieves data from the data base into a CSLA editable child business object of type <see cref="Account"/> /// using the criteria provided. /// </summary> /// <param name="criteria">Object of type <see cref="AccountCriteria"/></param> /// <returns></returns> private void Child_Fetch(AccountCriteria criteria) { bool cancel = false; OnChildFetching(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("SELECT [AccountId], [UniqueID], [Email], [FirstName], [LastName], [Address1], [Address2], [City], [State], [Zip], [Country], [Phone] FROM [dbo].[Account] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Account' using the following criteria: {0}.", criteria)); } } } } OnChildFetched(); }
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(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) { return; } if (OriginalItemId != ItemId) { // Insert new child. Item item = new Item { ItemId = ItemId, ProductId = ProductId, Status = Status, Name = Name, Image = Image }; if (ListPrice.HasValue) { item.ListPrice = ListPrice.Value; } if (UnitCost.HasValue) { item.UnitCost = UnitCost.Value; } if (Supplier.HasValue) { item.Supplier = Supplier.Value; } item = item.Save(); // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships. // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new ItemCriteria { ItemId = OriginalItemId }; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalItemId = ItemId; OnUpdated(); return; } 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 connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); 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", 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(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."); } LoadProperty(_originalItemIdProperty, this.ItemId); } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }