Ejemplo n.º 1
0
        internal static int GuessParameterNamePrefixLen(XmlCommandItem xmlCommand)
        {
            if (xmlCommand.Parameters.Count == 0)
            {
                return(0);
            }

            XmlCmdParameter param = xmlCommand.Parameters[0];

            int index = 0;

            for ( ; index < param.Name.Length; index++)
            {
                if (Char.IsLetter(param.Name[index]))
                {
                    break;
                }
            }

            if (index < param.Name.Length)
            {
                return(index);
            }
            else
            {
                return(0);
            }
        }
Ejemplo n.º 2
0
        private void btnAddCommand_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = treeView1.SelectedNode;

            if (selectedNode == null)
            {
                return;
            }

            TreeNode root = (selectedNode.IsFileNode() ? selectedNode : selectedNode.Parent);

            EditCommandDialog dlg = new EditCommandDialog();

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            XmlCommandItem command = dlg.Command;
            TreeNode       node    = new TreeNode(command.CommandName, Icon_CommandItem, Icon_CommandItem2);

            node.SetCommand(command);

            root.Nodes.Add(node);
            treeView1.SelectedNode = node;
            node.EnsureVisible();

            SetChangedAndRefreshUIAndTreeFocus();
        }
Ejemplo n.º 3
0
        public static List <XmlCommandItem> ImportCommandFormSP(string connectionString, string database, string spName)
        {
            DbParameter[] parameters = GetSpParameters(connectionString, database, spName);

            string spCode = GetProcedureCode(connectionString, database, spName);

            XmlCommandItem command = new XmlCommandItem();

            command.CommandName = spName;
            command.CommandText = GetSpCode(spCode);
            command.Database    = database;

            foreach (DbParameter para in parameters)
            {
                XmlCmdParameter myParam = new XmlCmdParameter();
                myParam.Name      = para.ParameterName;
                myParam.Type      = para.DbType;
                myParam.Direction = para.Direction;
                myParam.Size      = para.Size;
                command.Parameters.Add(myParam);
            }

            List <XmlCommandItem> list = new List <XmlCommandItem>(1);

            list.Add(command);
            return(list);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 创建新的XmlCommand对象。
        /// </summary>
        /// <param name="name">命令名字</param>
        /// <param name="argsObject">匿名对象表示的参数</param>
        /// <param name="replaces">替换的关键字字典</param>
        public XmlCommand(string name, object argsObject, Dictionary <string, string> replaces)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            XmlCommandItem command = XmlCommandManager.GetCommand(name);

            if (command == null)
            {
                throw new ArgumentOutOfRangeException("name", string.Format("指定的XmlCommand名称 {0} 不存在。", name));
            }

            // 根据XML的定义以及传入参数,生成SqlParameter数组
            SqlParameter[] parameters = GetParameters(command, argsObject);

            // 创建CPQuery实例
            StringBuilder commandText = new StringBuilder(command.CommandText);

            if (replaces != null)
            {
                foreach (KeyValuePair <string, string> kvp in replaces)
                {
                    commandText.Replace(kvp.Key, kvp.Value);
                }
            }


            _query = CPQuery.From(commandText.ToString(), parameters);
            _query.Command.CommandTimeout = command.Timeout;
            _query.Command.CommandType    = command.CommandType;
        }
Ejemplo n.º 5
0
        private void btnEditCommand_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            XmlCommandItem command = treeView1.SelectedNode.GetCommamd();

            if (command == null)
            {
                return;
            }


            EditCommandDialog dlg = new EditCommandDialog();

            dlg.Command = command;
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            command = dlg.Command;
            treeView1.SelectedNode.SetCommand(command);
            treeView1.SelectedNode.Text = command.CommandName;

            _changed = true;

            treeView1_AfterSelect(null, null);
        }
Ejemplo n.º 6
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            try {
                string xml = Clipboard.GetText();
                if (string.IsNullOrEmpty(xml))
                {
                    return;
                }


                if (xml.IndexOf(s_flagString) < 0)
                {
                    return;
                }


                XmlCommandItem command = XmlHelper.XmlDeserialize <XmlCommandItem>(xml);
                if (command != null)
                {
                    this.Command = command;
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally {
                btnImport.Visible = false;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 将XmlCommand对象中参数数组转换成与指定DbCommand兼容的命令参数数组。
        /// </summary>
        /// <param name="xmlCommand">XmlCommand对象</param>
        /// <param name="dbCommand">DbCommand对象</param>
        /// <returns>返回与指定DbCommand兼容的命令参数数组</returns>
        public static DbParameter[] GetCommandParameters(this XmlCommandItem xmlCommand, DbCommand dbCommand)
        {
            if (xmlCommand == null)
            {
                throw new ArgumentNullException("xmlCommand");
            }
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (xmlCommand.Parameters.Count == 0)
            {
                return(new DbParameter[0]);
            }

            DbParameter[] array = new DbParameter[xmlCommand.Parameters.Count];

            for (int i = 0; i < xmlCommand.Parameters.Count; i++)
            {
                XmlCmdParameter p = xmlCommand.Parameters[i];

                DbParameter para = dbCommand.CreateParameter();
                para.ParameterName = p.Name;
                para.DbType        = p.Type;
                para.Direction     = p.Direction;
                //if( p.Size != 0 )
                para.Size = p.Size;

                array[i] = para;
            }

            return(array);
        }
Ejemplo n.º 8
0
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                labMessage.Text = "Ready.";
                return;
            }
            labMessage.Text = treeView1.SelectedNode.FullPath;


            XmlCommandItem command = treeView1.SelectedNode.GetCommamd();

            if (command == null)
            {
                txtSQL.Text = string.Format("\r\n共 {0} 个命令子节点。", treeView1.SelectedNode.Nodes.Count);
                txtXML.Text = string.Empty;
            }
            else
            {
                string commandText = command.CommandText;

                command.CommandText = "....................";
                txtXML.Text         = XmlHelper.XmlSerialize(command, Encoding.UTF8);
                txtSQL.Text         = commandText;
                command.CommandText = commandText;
            }

            RefreshUI();
        }
Ejemplo n.º 9
0
        private void SetCommand(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            XmlCommandItem item = XmlCommandManager.GetCommand(name);

            if (item == null)
            {
                throw new ArgumentOutOfRangeException("name",
                                                      string.Format("指定的XmlCommand名称 {0} 不存在。", name));
            }

            // 填充命令对象
            _command.CommandText = item.CommandText;
            _command.CommandType = item.CommandType;

            if (item.Timeout > 0)
            {
                _command.CommandTimeout = item.Timeout;
            }

            FillParameters(item);
        }
Ejemplo n.º 10
0
        public string GetSql(string xmlcommandName)
        {
            // 这个测试类为了简单,就直接借用XmlCommand中定义的SQL语句

            XmlCommandItem x1 = XmlCommandManager.GetCommand(xmlcommandName);

            return(x1.CommandText);
        }
Ejemplo n.º 11
0
        public static string GenerateXmlCommandCallCode(XmlCommandItem xmlCommand, string spname, int prefixLen, bool namedType)
        {
            DbCommand dbCommand = new System.Data.SqlClient.SqlCommand();

            DbParameter[] parameters = xmlCommand.GetCommandParameters(dbCommand);

            string code = GenerateSpCallCode(parameters, spname, prefixLen, namedType);

            // 二者的调用方式完全一样,只是名称不一样而已。
            return(code.Replace("StoreProcedure", "XmlCommand"));
        }
Ejemplo n.º 12
0
        private XmlCommandItem GetXmlCommand(string name)
        {
            Init();

            XmlCommandItem command = XmlCommandManager.GetCommand(name);

            if (command == null)
            {
                throw new ArgumentOutOfRangeException("name", "不能根据指定的名称找到匹配的XmlCommand,name: " + name);
            }

            return(command);
        }
Ejemplo n.º 13
0
        private Dictionary <string, object> GetCommandParameters(XmlCommandItem command, NameValueCollection form)
        {
            Dictionary <string, object> table = new Dictionary <string, object>();

            foreach (var p in command.Parameters)
            {
                string paraName = p.Name.TrimStart('@');
                string text     = form[paraName];
                Type   t        = ConvertType(p.Type);
                object value    = _stringConverter.Instance.ToObject(text, t) ?? DBNull.Value;

                table[paraName] = value;
            }
            return(table);
        }
Ejemplo n.º 14
0
        private void menuGenerateCallCode_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null || treeView1.SelectedNode.IsFileNode())
            {
                return;
            }
            if (btnEditCommand.Enabled == false)
            {
                return;
            }

            XmlCommandItem command = treeView1.SelectedNode.GetCommamd();

            ShowCallCodeDialog dlg = new ShowCallCodeDialog(command, treeView1.SelectedNode.Text);

            dlg.Owner = this;
            dlg.Show();
        }
Ejemplo n.º 15
0
        public object Query(string x_name, [FromRequest] NameValueCollection form)
        {
            XmlCommandItem command = GetXmlCommand(x_name);

            Dictionary <string, object> args = null;

            if (command.Parameters != null || command.Parameters.Count > 0)
            {
                args = GetCommandParameters(command, form);
            }

            XmlCommand xmlCommand = XmlCommand.Create(x_name, args);
            DataTable  table      = xmlCommand.ToDataTable();

            // 处理输出参数,回填到参数字典中返回给前端
            BackFillOutputArgs(xmlCommand, args);

            return(new { args = args, data = table });
        }
Ejemplo n.º 16
0
        private void  制节点XMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }


            if (treeView1.SelectedNode.IsFileNode())
            {
                List <XmlCommandItem> list = treeView1.SelectedNode.GetCommandList();
                string xml = XmlHelper.XmlSerialize(list, Encoding.UTF8);
                Clipboard.SetText(xml);
            }
            else
            {
                XmlCommandItem command = treeView1.SelectedNode.GetCommamd();
                string         xml     = XmlHelper.XmlSerialize(command, Encoding.UTF8);
                Clipboard.SetText(xml);
            }
        }
Ejemplo n.º 17
0
        public object Execute(string x_name, [FromRequest] NameValueCollection form)
        {
            // name 这个参数名太常见,所以就改成 x_name

            XmlCommandItem command = GetXmlCommand(x_name);

            Dictionary <string, object> args = null;

            if (command.Parameters != null || command.Parameters.Count > 0)
            {
                args = GetCommandParameters(command, form);
            }

            XmlCommand xmlCommand = XmlCommand.Create(x_name, args);
            int        result     = xmlCommand.ExecuteNonQuery();

            // 处理输出参数,回填到参数字典中返回给前端
            BackFillOutputArgs(xmlCommand, args);

            return(new { args = args, data = result });
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 将XmlCommand中定义的参数填充到命令中
        /// </summary>
        /// <param name="item"></param>
        private void FillParameters(XmlCommandItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            foreach (XmlCmdParameter cp in item.Parameters)
            {
                DbParameter parameter = _command.CreateParameter();

                // 参数名需要和XmlCommand中的名字匹配,所以这里不做处理(不加前缀)
                parameter.ParameterName = cp.Name;
                parameter.DbType        = cp.Type;
                parameter.Direction     = cp.Direction;
                parameter.Size          = cp.Size;
                //parameter.Value = DBNull.Value;		// 先给默认值
                _command.Parameters.Add(parameter);
            }

            _item = item;                   // 记录从哪个 XmlCommandItem 对象构造出来的
        }
Ejemplo n.º 19
0
        public object Paging(string x_name, int pageIndex, int pageSize, [FromRequest] NameValueCollection form)
        {
            XmlCommandItem command = GetXmlCommand(x_name);

            Dictionary <string, object> args = null;

            if (command.Parameters != null || command.Parameters.Count > 0)
            {
                args = GetCommandParameters(command, form);
            }

            PagingInfo info = new PagingInfo {
                PageIndex = pageIndex, PageSize = pageSize
            };
            XmlCommand xmlCommand = XmlCommand.Create(x_name, args);
            DataTable  table      = xmlCommand.ToPageTable(info);

            // 处理输出参数,回填到参数字典中返回给前端
            BackFillOutputArgs(xmlCommand, args);

            return(new { args = args, paging = info, data = table });
        }
Ejemplo n.º 20
0
        public static List <XmlCommandItem> GetCUDCommandByTableName(string connectionString, string database, string tableName)
        {
            List <XmlCommandItem> commands = new List <XmlCommandItem>(3);

            List <Field> fields = GetFieldsFromTable(connectionString, database, tableName);

            List <string> primaryKeys = null;

            using (DbContext dbContext = CreateDbContext(connectionString, database)) {
                var parameter = new { TableName = tableName };
                primaryKeys = dbContext.CPQuery.Create(s_QueryPrimaryKeyScript, parameter).ToScalarList <string>();
            }

            if (primaryKeys.Count == 0)
            {
                string idFieldName = (from f in fields where f.Identity select f.Name).FirstOrDefault();
                if (idFieldName != null)
                {
                    primaryKeys.Add(idFieldName);
                }
            }

            if (primaryKeys.Count == 0)
            {
                string idFieldName = (from f in fields
                                      where string.Compare(f.DataType, "timestamp", StringComparison.OrdinalIgnoreCase) == 0
                                      select f.Name).FirstOrDefault();
                if (idFieldName != null)
                {
                    primaryKeys.Add(idFieldName);
                }
            }

            if (primaryKeys.Count == 0)
            {
                string idFieldName = (from f in fields
                                      where (f.DefaultValue != null && f.DefaultValue.IndexOf("newsequentialid()", StringComparison.OrdinalIgnoreCase) >= 0)
                                      select f.Name).FirstOrDefault();
                if (idFieldName != null)
                {
                    primaryKeys.Add(idFieldName);
                }
            }

            //if( primaryKeys.Count == 0 ) {		// 不能启用这段代码!
            //    string idFieldName = (from f in fields
            //                          where (f.DefaultValue != null && f.DefaultValue.IndexOf("newid()", StringComparison.OrdinalIgnoreCase) >= 0)
            //                          select f.Name).FirstOrDefault();
            //    if( idFieldName != null )
            //        primaryKeys.Add(idFieldName);
            //}


            XmlCommandItem insertCommand = new XmlCommandItem();

            insertCommand.CommandName = "Insert" + tableName.TrimPunctuation();
            insertCommand.CommandType = System.Data.CommandType.Text;

            StringBuilder sbInsert1 = new StringBuilder();
            StringBuilder sbInsert2 = new StringBuilder();

            sbInsert1.AppendFormat("\r\ninsert into [{0}] (", tableName);
            sbInsert2.Append("values (");

            foreach (Field f in fields)
            {
                if (f.Identity || f.Computed ||
                    string.Compare(f.DataType, "timestamp", StringComparison.OrdinalIgnoreCase) == 0 ||
                    (f.DefaultValue != null && f.DefaultValue.IndexOf("newsequentialid()", StringComparison.OrdinalIgnoreCase) >= 0))
                {
                    continue;
                }

                sbInsert1.AppendFormat("[{0}],", f.Name);
                sbInsert2.AppendFormat("@{0},", f.Name);
                insertCommand.Parameters.Add(ConvertToXmlCmdParameter(f));
            }
            sbInsert1.Remove(sbInsert1.Length - 1, 1).AppendLine(")");
            sbInsert2.Remove(sbInsert2.Length - 1, 1).AppendLine(");");
            insertCommand.CommandText = sbInsert1.ToString() + sbInsert2.ToString();

            commands.Add(insertCommand);



            if (primaryKeys.Count > 0)
            {
                XmlCommandItem updateCommand = new XmlCommandItem();
                updateCommand.CommandName = "Update" + tableName.TrimPunctuation();
                updateCommand.CommandType = System.Data.CommandType.Text;

                StringBuilder sbUpdate = new StringBuilder();
                sbUpdate.AppendFormat("\r\nupdate [{0}] set \r\n", tableName);

                foreach (Field f in fields)
                {
                    if (f.Identity || f.Computed ||
                        string.Compare(f.DataType, "timestamp", StringComparison.OrdinalIgnoreCase) == 0 ||
                        (f.DefaultValue != null && f.DefaultValue.IndexOf("newsequentialid()", StringComparison.OrdinalIgnoreCase) >= 0))
                    {
                        continue;
                    }

                    if (DataExtensions.FindIndex(primaryKeys, f.Name) >= 0)
                    {
                        continue;
                    }

                    sbUpdate.AppendFormat("[{0}] = @{0}, ", f.Name);
                    updateCommand.Parameters.Add(ConvertToXmlCmdParameter(f));
                }
                sbUpdate.Remove(sbUpdate.Length - 2, 2).Append("\r\nWhere ");

                foreach (Field f in fields)
                {
                    if (DataExtensions.FindIndex(primaryKeys, f.Name) < 0)
                    {
                        continue;
                    }

                    sbUpdate.AppendFormat(" [{0}] = @{0} and", f.Name);
                    updateCommand.Parameters.Add(ConvertToXmlCmdParameter(f));
                }
                sbUpdate.Remove(sbUpdate.Length - 4, 4).AppendLine();
                updateCommand.CommandText = sbUpdate.ToString();

                commands.Add(updateCommand);



                XmlCommandItem deleteCommand = new XmlCommandItem();
                deleteCommand.CommandName = "Delete" + tableName.TrimPunctuation();
                deleteCommand.CommandType = System.Data.CommandType.Text;

                StringBuilder sbDelete = new StringBuilder();
                sbDelete.AppendFormat("\r\ndelete from [{0}] where \r\n", tableName);

                foreach (Field f in fields)
                {
                    if (DataExtensions.FindIndex(primaryKeys, f.Name) < 0)
                    {
                        continue;
                    }

                    sbDelete.AppendFormat(" [{0}] = @{0} and", f.Name);
                    deleteCommand.Parameters.Add(ConvertToXmlCmdParameter(f));
                }
                sbDelete.Remove(sbDelete.Length - 4, 4).AppendLine();
                deleteCommand.CommandText = sbDelete.ToString();

                commands.Add(deleteCommand);
            }

            return(commands);
        }
Ejemplo n.º 21
0
 public ShowCallCodeDialog(XmlCommandItem command, string nodeText)
 {
     InitializeComponent();
     _command  = command;
     _nodeText = nodeText;
 }
 public static void SetCommand(this TreeNode node, XmlCommandItem command)
 {
     node.Tag = command;
 }