private void AssignOutputValuesToInstance(CommandMapping cmdMap, SqlCommand currentCmd, ref PersistableObject persistObject)
        {
            Type         objType;
            PropertyInfo prop;

            objType = persistObject.GetType();
            SqlParameter curParam;

            foreach (CommandParameter cmdParameter in cmdMap.Parameters)
            {
                if ((cmdParameter.RealParameterDirection == ParameterDirection.InputOutput) ||
                    (cmdParameter.RealParameterDirection == ParameterDirection.ReturnValue) ||
                    (cmdParameter.RealParameterDirection == ParameterDirection.Output))
                {
                    curParam = currentCmd.Parameters[cmdParameter.ParameterName];
                    prop     = objType.GetProperty(cmdParameter.ClassMember);
                    if (prop != null)
                    {
                        if (curParam.Value != DBNull.Value)                          //如果传出参数的值不为空
                        {
                            prop.SetValue(persistObject, curParam.Value, null);      //为当前属性设置值
                        }
                    }
                }
            }
        }
        private void AssignReturnValueToDataSet(CommandMapping cmdMap, SqlCommand currentCmd, ref PersistableObject persistObject)
        {
            DataSet        internalData = persistObject.ResultSet;
            SqlDataAdapter sqlDa        = new SqlDataAdapter(currentCmd);

            sqlDa.Fill(internalData);
        }
        public override void Execute(PersistableObject currentObject)
        {
            try
            {
                CommandMapping cmdMap         = (CommandMapping)ContainerMapping.CommandMappingList[ContainerMapping.CurrentCommandName];
                SqlCommand     currentCommand = BuildCommandFromMapping(cmdMap);              //根据一个Command映射对象来创建一个SqlCommand对象
                AssignValuesToParameters(cmdMap, ref currentCommand, currentObject);
                currentCommand.Connection.Open();

                if (cmdMap.ReturnValueType == "DataSet")
                {
                    AssignReturnValueToDataSet(cmdMap, currentCommand, ref currentObject);
                    AssignOutputValuesToInstance(cmdMap, currentCommand, ref currentObject);
                    currentCommand.Connection.Close();
                }
                else
                {
                    currentCommand.ExecuteNonQuery();
                    currentCommand.Connection.Close();
                    AssignOutputValuesToInstance(cmdMap, currentCommand, ref currentObject);
                }
                currentCommand.Connection.Dispose();
                currentCommand.Dispose();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        private void AssignValuesToParameters(CommandMapping cmdMap, ref SqlCommand currentCmd, PersistableObject persistObject)
        {
            Type         objType;
            PropertyInfo prop;

            objType = persistObject.GetType();

            foreach (CommandParameter cmdParameter in cmdMap.Parameters)
            {
                if ((cmdParameter.RealParameterDirection == ParameterDirection.Input) ||
                    (cmdParameter.RealParameterDirection == ParameterDirection.InputOutput))
                {
                    prop = objType.GetProperty(cmdParameter.ClassMember);                                         //得到objType类型中属性名为cmdParameter.ClassMember的一个属性对象
                    currentCmd.Parameters[cmdParameter.ParameterName].Value = prop.GetValue(persistObject, null); //将persistObject对象中prop所表示的属性的值作为当前sql command对象所对应的参数的值
                }
            }
        }
Exemple #5
0
        protected static CommandMapping CreateCommandMappingFromNode(string commandName, XmlNode cmdNode)
        {
            CommandMapping newCmdMap = new CommandMapping(commandName);

            newCmdMap.ReturnValueType = cmdNode.SelectSingleNode("ReturnValueType").InnerText;
            //创建这个命令映射对象的参数列表
            CommandParameter newParam;

            foreach (XmlNode cmdParamNode in cmdNode.SelectNodes("Parameter"))
            {
                newParam                = new CommandParameter();
                newParam.ClassMember    = cmdParamNode.SelectSingleNode("ClassMember").InnerText;
                newParam.DbTypeHint     = cmdParamNode.SelectSingleNode("DbTypeHint").InnerText;
                newParam.ParameterName  = cmdParamNode.SelectSingleNode("ParameterName").InnerText;
                newParam.Size           = System.Convert.ToInt32(cmdParamNode.SelectSingleNode("Size").InnerText);
                newParam.ParamDirection = cmdParamNode.SelectSingleNode("ParamDirection").InnerText;
                newCmdMap.AddParameter(newParam);
            }

            return(newCmdMap);
        }
        private SqlCommand BuildCommandFromMapping(CommandMapping cmdMap)
        {
            SqlConnection conn = new SqlConnection(SiteProfile.DefaultDataSource);

            SqlCommand sqlCommand = conn.CreateCommand();

            sqlCommand.CommandText = cmdMap.CommandName;
            sqlCommand.CommandType = CommandType.StoredProcedure;
            foreach (CommandParameter cmdParameter in cmdMap.Parameters)
            {
                SqlParameter newParam = new SqlParameter();
                newParam.ParameterName = cmdParameter.ParameterName;
                newParam.Direction     = cmdParameter.RealParameterDirection;
                newParam.SqlDbType     = (SqlDbType)SiteProfile.DbTypeHints[cmdParameter.DbTypeHint];
                newParam.Size          = cmdParameter.Size;

                sqlCommand.Parameters.Add(newParam);
            }

            return(sqlCommand);
        }