Ejemplo n.º 1
0
        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;
                }
            }
        }
Ejemplo n.º 2
0
        private void DataPortal_Fetch(CategoryCriteria criteria)
        {
            bool cancel = false;

            OnFetching(criteria, ref cancel);
            if (cancel)
            {
                return;
            }

            RaiseListChangedEvents = false;

            // Fetch Child objects.
            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("[dbo].[CSLA_Category_Select]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag));
                    command.Parameters.AddWithValue("@p_NameHasValue", criteria.NameHasValue);
                    command.Parameters.AddWithValue("@p_DescnHasValue", criteria.DescriptionHasValue);
                    using (var reader = new SafeDataReader(command.ExecuteReader()))
                    {
                        if (reader.Read())
                        {
                            do
                            {
                                this.Add(PetShop.Tests.Collections.EditableRoot.Category.GetCategory(reader));
                            } while(reader.Read());
                        }
                    }
                }
            }

            RaiseListChangedEvents = true;

            OnFetched();
        }
        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.

                // 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;
            }

            using (var connection = new SqlConnection(ADOHelper.ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("[dbo].[CSLA_Category_Update]", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    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);
                }
            }

            OnUpdated();
        }