DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 發布一個原創的基於Ajax的通用(組合)查詢
發布一個原創的基於Ajax的通用(組合)查詢
編輯:AJAX詳解      日期:2016/12/30 17:49:31

簡單介紹:
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>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved