private void AssignReturnValue(SqlCommand currentCmd, PersistableObject persistObject)
 {
     foreach (SqlParameter parameter in currentCmd.Parameters)
     {
         if (parameter.Direction == ParameterDirection.ReturnValue)
         {
             persistObject.ReturnValue = parameter.Value;
         }
     }
 }
        public override void Execute(string commandName, PersistableObject currentObject)
        {
            if (currentObject == null)
            {
                throw new PersistableObjectNullException();
            }
            CommandMapping commandMapping = null;

            for (int i = 0; i < CMPConfigurationHandler.ContainerMaps.Count; i++)
            {
                commandMapping = CMPConfigurationHandler.ContainerMaps[i].CommandMappingList[commandName] as CommandMapping;

                if (commandMapping != null)
                {
                    break;
                }
            }

            if (commandMapping == null)
            {
                throw new CommandMappingWithoutContainerMappingIdNotFoundException(commandName);
            }

            SqlCommand currentCommand = BuildCommandFromMapping(commandMapping);

            AssignValuesToParameters(commandMapping, ref currentCommand, currentObject);
            currentCommand.Connection.Open();

            AssignReturnValueToDataSet(currentCommand, ref currentObject);

            AssignOutputValuesToInstance(commandMapping, currentCommand, ref currentObject);

            AssignReturnValue(currentCommand, currentObject);

            currentCommand.Connection.Close();
            currentCommand.Connection.Dispose();
            currentCommand.Dispose();
        }
Example #3
0
 /// <summary>
 /// 提供一个持久性容器名称和该容器中的一个存储过程以及一个持久性对象进行数据访问,三个参数都不能为空。
 /// </summary>
 /// <param name="containerMappingId">持久性容器ID</param>
 /// <param name="commandName">存储过程的名称</param>
 /// <param name="currentObject">一个持久性对象</param>
 public virtual void Execute(string containerMappingId, string commandName, PersistableObject currentObject)
 {
 }
Example #4
0
 /// <summary>
 /// 通过一个存储过程的名称和一个持久性对象进行数据访问,这种方式的前提是所有的存储过程的名称都不重复。
 /// </summary>
 /// <param name="commandName">存储过程的名称</param>
 /// <param name="currentObject">一个持久性对象,不能为空</param>
 public virtual void Execute(string commandName, PersistableObject currentObject)
 {
 }
        private void AssignValuesToParameters(CommandMapping cmdMap, ref SqlCommand currentCmd, PersistableObject persistObject)
        {
            ParameterDirection parameterDirection;

            foreach (CommandParameter cmdParameter in cmdMap.Parameters)
            {
                parameterDirection = cmdParameter.RealParameterDirection;

                if (parameterDirection == ParameterDirection.Input)
                {
                    PropertyInfo propertyInfo = persistObject.GetType().GetProperty(cmdParameter.ClassMember);
                    if (propertyInfo == null)
                    {
                        throw new PropertyNotFoundException(cmdParameter.ClassMember);
                    }
                    object propertyValue = propertyInfo.GetValue(persistObject, null);
                    currentCmd.Parameters[cmdParameter.ParameterName].Value = propertyValue;
                }
            }
        }
        private void AssignOutputValuesToInstance(CommandMapping cmdMap, SqlCommand currentCmd, ref PersistableObject persistObject)
        {
            SqlParameter       curParam;
            ParameterDirection parameterDirection;

            foreach (CommandParameter cmdParameter in cmdMap.Parameters)
            {
                parameterDirection = cmdParameter.RealParameterDirection;

                if (parameterDirection == ParameterDirection.Output)
                {
                    curParam = currentCmd.Parameters[cmdParameter.ParameterName];

                    if (curParam.Value != DBNull.Value)
                    {
                        PropertyInfo propertyInfo = persistObject.GetType().GetProperty(cmdParameter.ClassMember);
                        if (propertyInfo == null)
                        {
                            throw new PropertyNotFoundException(cmdParameter.ClassMember);
                        }
                        propertyInfo.SetValue(persistObject, curParam.Value, null);
                    }
                }
            }
        }
        private void AssignReturnValueToDataSet(SqlCommand currentCmd, ref PersistableObject persistObject)
        {
            SqlDataAdapter sqlDa = new SqlDataAdapter(currentCmd);

            sqlDa.Fill(persistObject.ResultSet);
        }