ajax +javascript 查询数据库

来源:互联网 发布:卷皮版淘宝客程序 编辑:程序博客网 时间:2024/06/06 02:16

 

private void Common_AjaxGetBrowseData()

    {

        string strBindDatabase = HttpContext.Current.Server.UrlDecode(Request["strBindDatabase"]);

        string pageTablename = HttpContext.Current.Server.UrlDecode(Request["pageTablename"]);

        string pageFields = HttpContext.Current.Server.UrlDecode(Request["pageFields"]);

        string strwhere = Request["strwhere"];

        string pageIndex = HttpContext.Current.Server.UrlDecode(Request["pageIndex"]);

        string pageSize = HttpContext.Current.Server.UrlDecode(Request["pageSize"]);

        string doCount = HttpContext.Current.Server.UrlDecode(Request["doCount"]);

        DataSet ds = GetBrowseData(strBindDatabase, pageTablename, pageFields, strwhere, Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), Convert.ToInt32(doCount));

        Response.ContentType = "text/xml";

        Response.Charset = "UTF-8";

        Response.Write("<?xml version=/"1.0/" encoding=/"UTF-8/" ?>" + "/n");

        //=====update by agan 2010-6-25==================

        //因为当值为null时,在导出成XML文件的时候,该列没有,所在在这里遍历DataSet,把null全部替换为空。

 

        foreach (DataTable dt in ds.Tables)

        {

            foreach (DataRow dr in dt.Rows)

            {

                foreach (DataColumn dc in dr.Table.Columns)

                {

                    if (dr[dc] == DBNull.Value || dr[dc]==null)

                    {

                        dr[dc] = "";

                    }

                }

            }

        }

        //========================

        Response.Write(ds.GetXml());

        Response.End();

    }

 

 

 

 

 public DataSet GetBrowseData(string strBindDatabase, string tablename, string fields, string where, int pageIndex, int pageSize, int doCount)

    {

        if (strBindDatabase != "(local)")

        {

            string strSQL = "select " + fields + " from " + tablename;

            if (where != "")

                strSQL += " where " + where;

            ConnectionClass objBPOCn = new ConnectionClass();

            objBPOCn.Open2(common.BPOSever, UserSession.Current.LoginAccount, UserSession.Current.LoginPwd, common.BPOPort, true, true);

            BPOLib.IDSCClass cc = new IDSCClass();

            cc.Open(objBPOCn, strBindDatabase);

            string strCon = @"DSN=" + cc.DSN + ";Uid=" + cc.UID + ";Pwd=" + cc.Pwd;

            Response.Write(" <script> alert( ' " + strSQL + " '); </script> ");

            System.Data.Odbc.OdbcConnection testq = new System.Data.Odbc.OdbcConnection(strCon);

            testq.Open();

            OdbcDataAdapter da = new OdbcDataAdapter(strSQL, testq);

            OdbcCommandBuilder myCB = new OdbcCommandBuilder(da);

            DataSet ds = new DataSet();

            da.Fill(ds);

            testq.Close();

            objBPOCn.Close();

            return ds;

        }

 

    }

 

 

    function GetOffice() {

        var wh = "B21ID=" + document.getElementById("com").value;

 

        var strPara = "type=GetBrowseData&strBindDatabase=" +

 encodeURIComponent("Media System Database ODBC") + "&pageTablename=" + encodeURIComponent("COMPANY") +

 "&pageFields=" + encodeURIComponent("COMPANY_ID , B21ID , CNAME") + "&strWhere=" + encodeURIComponent(wh) +

 "&pageIndex=1&pageSize=1&doCount=1";

 

        //                  location.href = "http://10.64.56.7/eprocess/forms/CallAjax.aspx?" + strPara;

 

        var xmlHttp = CreateXMLHttpRequest();

        xmlHttp.open('POST', "CallAjax.aspx", false); // 注意第一个参数

        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        xmlHttp.send(strPara); // 发送数据

 

        var result = xmlHttp.status;

        if (xmlHttp.readyState == 4) {

            if (result == 200) {//成功返回,返回对象值xmlHttp

                //这里再把返回的字符串进行处理

                CallA(xmlHttp);

                //                CallBackFormDataByPid(xmlHttp); //这个方法可以自己写一个,

                xmlHttp = null;

            }

            else {

                xmlHttp = null;

            }

        }

 

 

    }

 

 

    function CallA(res) {

 

        if (res == null || res.responseXML.getElementsByTagName("Table").length == 0)

            return;

 

 

        var slt = document.getElementById("office");

        slt.options.length = 0;     //清空下拉框里的数据

        slt.add(new Option("--请选择--", -1));        //添加提示

        var tables = res.responseXML.getElementsByTagName("COMPANY_ID");

        for (var i = 0; i < tables.length; i++) {

 

            var e = document.createElement("OPTION");

            e.innerText = res.responseXML.getElementsByTagName('CNAME')[i].firstChild.data;

            e.value = res.responseXML.getElementsByTagName('COMPANY_ID')[i].firstChild.data;

            slt.appendChild(e);

        }

    }