Пример #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);
        }
Пример #2
0
        //执行最后的sql
        private string ExecSqlJms(InterfaceModel response, IMicroService service, JmsCommand dbCommand, 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   = service.Invoke <object>("ExecuteScalar", dbCommand);
                dataResult           = dataResult1;
                result = dataResult1.Result;
                break;

            case "Int":
                IntDataResult intDataResult = new IntDataResult();
                intDataResult.Result   = service.Invoke <int>("ExecuteNonQuery", dbCommand);
                intDataResult.DataType = 200;
                dataResult             = intDataResult;
                result = intDataResult.Result;
                break;

            case "DataSet":
                DataSet      ds           = new DataSet();
                DSDataResult dSDataResult = new DSDataResult();
                if (filters != null)
                {
                    //复杂查询需要再过滤一下最后一个表
                    int    lastIndex = ds.Tables.Count - 1;
                    string msg;
                    string filter = this.CreateFileterString(filters, out msg);
                    if (msg != "")
                    {
                        throw new Exception(msg);
                    }

                    ds = service.Invoke <DataSet>("Fill", dbCommand);
                    if (ds.Tables.Count > 0 && ds.Tables[lastIndex].Rows.Count > 0)
                    {
                        DataTable table = ds.Tables[ds.Tables.Count - 1];
                        table.DefaultView.RowFilter = filter;
                        DataTable dtNew = table.DefaultView.ToTable();
                        ds.Tables.RemoveAt(lastIndex);
                        ds.Tables.Add(dtNew);
                        dSDataResult.Result = ds;
                    }
                    else
                    {
                        dSDataResult.Result = ds;
                    }
                }
                else
                {
                    ds = service.Invoke <DataSet>("Fill", dbCommand);
                    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);
        }