Ejemplo n.º 1
0
        //执行最后的sql
        private string ExecSql(InterfaceModel response, IDbConnection connection, IDbDataAdapter sqlDA, IDbCommand cmd, List <FilterCondition> filters)
        {
            XmlHelper xmlHelp    = new XmlHelper();
            string    json       = "";
            object    dataResult = new object();
            object    result     = new object();

            switch (response.ExecuteType)
            {
            case "Scalar":
                DataResult dataResult1 = new DataResult();
                dataResult1.DataType = 200;
                dataResult1.Result   = cmd.ExecuteScalar();
                dataResult           = dataResult1;
                result = dataResult1.Result;
                break;

            case "Int":
                IntDataResult intDataResult = new IntDataResult();
                intDataResult.Result   = cmd.ExecuteNonQuery();
                intDataResult.DataType = 200;
                dataResult             = intDataResult;
                result = intDataResult.Result;
                break;

            case "DataSet":
                DataSet      ds           = new DataSet();
                DSDataResult dSDataResult = new DSDataResult();
                sqlDA.Fill(ds);
                int lastIndex = ds.Tables.Count - 1;

                if (filters != null)
                {
                    //复杂查询需要再过滤一下最后一个表

                    string msg;
                    string filter = this.CreateFileterString(filters, out msg);
                    if (msg != "")
                    {
                        throw new Exception(msg);
                    }

                    if (ds.Tables.Count > 0 && ds.Tables[lastIndex].Rows.Count > 0)
                    {
                        //只过滤最后一个表
                        DataTable table = ds.Tables[lastIndex];
                        table.DefaultView.RowFilter = filter;
                        DataTable dtNew = table.DefaultView.ToTable();
                        ds.Tables.RemoveAt(lastIndex);
                        ds.Tables.Add(dtNew);
                    }

                    dSDataResult.Result = ds;
                }
                else
                {
                    int nCount = 0;
                    if (this.context.Request.Headers.ContainsKey("NCS"))
                    {
                        bool bOK = context.Request.Headers.TryGetValue("NCS", out StringValues NCS);
                        if (bOK)
                        {
                            nCount = int.Parse(NCS.ToString());
                        }
                    }

                    if (nCount > 0 && lastIndex >= 0)
                    {
                        DataTable dtNew = DtSelectTop(nCount, ds.Tables[lastIndex]);
                        ds.Tables.RemoveAt(lastIndex);
                        ds.Tables.Add(dtNew);
                    }
                    dSDataResult.Result = ds;
                }
                dataResult            = dSDataResult;
                dSDataResult.DataType = 200;
                break;
            }

            if (response.SerializeType == "Xml")
            {
                switch (response.ExecuteType)
                {
                case "Scalar":
                    json = xmlHelp.SerializeXML <DataResult>(dataResult);
                    break;

                case "Int":
                    json = xmlHelp.SerializeXML <IntDataResult>(dataResult);
                    break;

                case "DataSet":
                    json = xmlHelp.SerializeXML <DSDataResult>(dataResult);
                    break;
                }
            }
            else if (response.SerializeType == "Json")
            {
                json = JsonConvert.SerializeObject(dataResult);
            }
            else
            {
                json = result.ToString();
            }

            return(json);
        }
Ejemplo n.º 2
0
        //返因LayUI的结构
        private string ExecSqlLayui(InterfaceModel response, IDbConnection connection, IDbDataAdapter sqlDA, IDbCommand cmd, List <FilterCondition> filters)
        {
            XmlHelper   xmlHelp    = new XmlHelper();
            string      json       = "";
            LayuiResult dataResult = new LayuiResult();

            dataResult.code  = 0;
            dataResult.count = 0;
            dataResult.msg   = "";
            dataResult.data  = "{0}";

            object result = new object();

            switch (response.ExecuteType)
            {
            case "Scalar":
                result = cmd.ExecuteScalar();
                break;

            case "Int":
                result = cmd.ExecuteNonQuery();
                break;

            case "DataSet":
                DataSet ds = new DataSet();
                sqlDA.Fill(ds);
                DataTable table = new DataTable();
                if (ds.Tables.Count > 0)
                {
                    if (filters != null)
                    {
                        //复杂查询需要再过滤一下最后一个表
                        string msg;
                        string filter = this.CreateFileterString(filters, out msg);
                        if (msg != "")
                        {
                            throw new Exception(msg);
                        }

                        //只过滤最后一个表
                        DataTable table0 = ds.Tables[ds.Tables.Count - 1];
                        table0.DefaultView.RowFilter = filter;
                        DataTable dtNew = table0.DefaultView.ToTable();
                        table = dtNew;
                    }
                    else
                    {
                        table = ds.Tables[0];
                    }

                    //测试数据最返最后10行
                    int nCount = 0;
                    if (this.context.Request.Headers.ContainsKey("NCS"))
                    {
                        bool bOK = context.Request.Headers.TryGetValue("NCS", out StringValues NCS);
                        if (bOK)
                        {
                            nCount = int.Parse(NCS.ToString());
                        }
                    }

                    if (nCount > 0)
                    {
                        table = DtSelectTop(nCount, table);
                    }
                }

                dataResult.count = table.Rows.Count;
                result           = table;
                break;
            }

            dataResult.data = result;
            if (response.SerializeType == "Xml")
            {
            }
            else if (response.SerializeType == "Json")
            {
                json = JsonConvert.SerializeObject(dataResult);
            }
            else
            {
                json = result.ToString();
            }

            return(json);
        }