发布一个原创的基于Ajax的通用(组合)查询

80酷酷网    80kuku.com

  ajax|原创

简单介绍:
1.完全无刷实现,客户端主要实现生成条件选择或录入控件,并进行录入控制;

2.将字段信息存放于XML文件,便于数据移植

3.通过js脚本,ajax框架引擎获取字段信息

4.通过js脚本生成组合条件;

 


 
5.运行效果:

下面是代码和实现效果:
1.AjaxSeachMethod.cs

using System;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
 
namespace WebUI.AjaxSearchSystem
{
    /**//// <summary>
    /// SearchMethod 的摘要说明。
    /// </summary>
    public class AjaxSearchMethod
    {
        private string[] CHAR_OP    = new string[]{"等于|=","不等于|<>","相似于|Like","不相似于|Not Like"};
        private string[] NUM_OP        = new string[]{"大于|>","大于或等于|>=","等于|=","不等于|<>","小于|<","小于或等于|<="};    
        private string[] DATE_OP    = new string[]{"等于|=","不等于|<>","先于|<","后于|>"};    
 
        public AjaxSearchMethod()
        {}
 
        /**//// <summary>
        /// 根据表名获取包含的字段
        /// </summary>
        /// <param name="table">表名</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string[] GetFields(string table)
        {
            string[] strArrFields = null;
 
            try
            {
                XmlDocument xd = LoadXml("\AjaxSearchDataCollection.xml");
                XmlElement    xe  =xd.DocumentElement;
                XmlNode        xn = xe.SelectSingleNode("descendant::Table[value='"+table+"']");
                XmlNodeList xnl = xn.ChildNodes;
                int iLen = xnl.Count;
                strArrFields = new string[iLen];
 
                int iIndex = 0;
 
                foreach(XmlNode xnField in xnl)
                {
                    strArrFields[iIndex] = xnField.Attributes["Name"].InnerText + "|" + xnField.Attributes["Value"].InnerText;
                    iIndex ++;
                }
            }
            catch(Exception ee)
            {
                throw new ArgumentOutOfRangeException("AjaxSearchDataCollection.xml","/Table[value='"+table+"']:指定的接点不存在,请重新定义XML文档!");
            }
            return strArrFields;
        }
 
        /**//// <summary>
        /// 根据字段获取对应的操作符列表
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string[] GetOpts(string table,string field)
        {
            string strTypeofField = GetTypeofField(table,field);
 
            switch(strTypeofField)
            {
                case "char":
                    return CHAR_OP;
                    break;
                case "int":
                    if(GetEnumField(table,field)==null)
                    {
                        return NUM_OP;
                    }
                    else
                    {
                        return (new string[]{"是|=","不是|<>"});
                    }
                    
                    break;
                case "decimal":
                    return NUM_OP;
                    break;
                case "datetime":
                    return DATE_OP;
                    break;
                default:
                    return (new string[]{"等于|="});
                    break;
            }
        }
 
        /**//// <summary>
        /// 根据字段获取对应的可能存在的枚举值列表
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string[] GetEnums(string table,string field)
        {
            string strTypeofField = GetTypeofField(table,field);
            string[] enums = GetEnumField(table,field);
 
            int iLen = enums==null?0:enums.Length;
 
            string[] result = new string[iLen+1];
 
            result[0] = strTypeofField;
 
            if (iLen == 0)
            {
                return result;
            }
 
            int iIndex = 1;
 
            foreach(string str in enums)
            {
                result[iIndex] = str;
                iIndex ++;
            }
 
            return result;
 
        }
 
        内部支持脚本#region 内部支持脚本
        //装载XML文档
        private XmlDocument LoadXml(string xmlPath)
        {
            XmlDocument xd = new XmlDocument();
 
            try
            {
                xd.Load(System.Web.HttpContext.Current.Request.PhysicalApplicationPath+xmlPath);
            }
            catch(XmlException ee)
            {
                throw new ArgumentOutOfRangeException(ee.Message,"读取条件编辑信息失败,请检查XML文档路径设置是否正确!");
            }
 
            return xd;
        }
 
        //获取字段的数据类型
        private string GetTypeofField(string table,string field)
        {
            string strTypeofField = string.Empty;
            try
            {
                XmlDocument xd = LoadXml("\\AjaxSearchDataCollection.xml");
                XmlElement    xe  =xd.DocumentElement;
                XmlNode        xn = xe.SelectSingleNode("descendant::Table[value='"+table+"']");
                xn = xn.SelectSingleNode("descendant::Field[Value='"+field+"']");
                
                strTypeofField = xn.Attributes["DataType"].InnerText;
                
            }
            catch(Exception ee)
            {
                throw new ArgumentOutOfRangeException("AjaxSearchDataCollection.xml","/Field[Value='"+field+"']:指定的接点不存在,请重新定义XML文档!");
            }
 
            return strTypeofField.ToLower();
        }
 
        //获取字段的数据类型
        private string[] GetEnumField(string table,string field)
        {
            string strTypeofField = string.Empty;
            string strEnumSqlSyntax = string.Empty;
            string strTextField = string.Empty;
            string strValueField = string.Empty;
 
            try
            {
                XmlDocument xd = LoadXml("\AjaxSearchDataCollection.xml");
                XmlElement    xe  =xd.DocumentElement;
                XmlNode        xn = xe.SelectSingleNode("descendant::Table[value='"+table+"']");
                xn = xn.SelectSingleNode("descendant::Field[Value='"+field+"']");
                xn = xn.ChildNodes[0];
 
                if (xn == null)
                {
                    return null;
                }
                else
                {
                    strEnumSqlSyntax = xn.Attributes["SqlSyntax"]==null?"":xn.Attributes["SqlSyntax"].InnerText;
                    strTextField = xn.Attributes["TextField"]==null?"":xn.Attributes["TextField"].InnerText;
                    strValueField = xn.Attributes["ValueField"]==null?"":xn.Attributes["ValueField"].InnerText;
                }
 
                return GetEnumField(strEnumSqlSyntax,strTextField,strValueField);
            }
            catch(NullReferenceException ee)
            {
                throw new ArgumentOutOfRangeException("AjaxSearchDataCollection.xml","/Field[Value='"+field+"']:指定的接点不存在,请重新定义XML文档!");
            }
        }
 
        private string[] GetEnumField(string sqlSyntax,string text,string value)
        {
 
            if (sqlSyntax.Length == 0 || text.Length==0 || value.Length==0)
            {
                return null;
            }
 
            DataSet ds = new DataSet();
 
            try
            {
                SqlConnection sc = new SqlConnection("server=OY-M;User ID=sa;Password=DBCs%1+ZhLx;database=pubs;Connection Reset=FALSE;Connect Timeout=6");
                sc.Open();
                SqlCommand scmd = new SqlCommand(sqlSyntax,sc);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = scmd;
                da.Fill(ds);
            }
            catch(SqlException ee)
            {
                throw ee;
            }
 
            DataTable dt = ds.Tables[0];
 
            if (dt == null)
            {
                return null;
            }
 
            DataRowCollection drc = dt.Rows;
            
            string[] enumFields = new string[drc.Count];
            int iIndex = 0;
 
            foreach(DataRow dr in drc)
            {
                enumFields[iIndex] = dr[text].ToString().Trim() +"|"+dr[value].ToString().Trim();
                iIndex ++;
            }
 
            return enumFields;
        }
        #endregion
    }

 


2.AjaxSearchDataCollection.xml

<?xml version="1.0" encoding="utf-8" ?>
<Fields>
    <Table Name="产品表" value="employee">
        <Field Name="雇佣号" Value="emp_id" DataType="char">
        </Field>
        <Field Name="名字" Value="fname" DataType="char">
            <Enum SqlSyntax="" TextField="" ValueField=""></Enum>
        </Field>
        <Field Name="工作号" Value="job_id" DataType="int">
            <Enum SqlSyntax="SELECT job_id,job_desc FROM jobs" TextField="job_desc" ValueField="job_id"></Enum>
        </Field>
        <Field Name="工作**" Value="job_lvl" DataType="int">
        </Field>
        <Field Name="雇佣日期" Value="hire_date" DataType="datetime">
        </Field>
    </Table>
</Fields>



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: