将sql查询作为数据传递到highchart

时间:2019-02-06 19:07:06

标签: c# sql-server

我正在Visual Studio中将asp.net用于webAPI,在其中我想从数据库中检索一些数据并将其传递到另一个对象中。

我的js图表已经在获取API数据,因此查询必须动态提供它

就目前而言,我的查询结果是一列包含14行数据(每月一个,并与上一年和下一年进行比较),并且我的图表接收到一个数组。

这就是我所拥有的

            public class MPreventivasAXEController : ApiController
            {

                public static void createCommand()
                {
                    ConnectionFactory db = new ConnectionFactory();
                    db.abrirConexao();

                    SqlCommand cmd = new SqlCommand();

                    string sqlAbertas = "SELECT COUNT(*) AS mes" +
                                        "FROM WorkOrder WHERE workOrderSource = '02'" +
                                        "AND YEAR(workOrderDate)=2018" +
                                        "AND conclusionDate IS NULL" +
                                        "GROUP BY MONTH(workOrderDate)";
                    cmd.Connection = db.conn;
                    cmd.CommandText = sqlAbertas;


                    SqlDataReader res = cmd.ExecuteReader();
                    int i = 0;
                    int[] obj = new int[] { };
                    while (res.Read())
                    {
                        int temp;

                        int.TryParse(res["mes"].ToString(), out temp);
                        obj[i] = temp;
                    }
                }

                Grafico grafico = new Grafico
                {
                    categories = new string[] {
                    "Média 2016"
                    , "Jan"
                    , "Fev"
                    , "Mar"
                    , "Abril"
                    , "Maio"
                    , "Jun"
                    , "Jul"
                    , "Ago"
                    , "Set"
                    , "Out"
                    , "Nov"
                    , "Dez"
                    , "Média 2017"
                },

                    series = new Serie[] {
                        new Serie
                        {
                            name = "Abertas"
                            , data = new int[] {
                                /* 3757, 3880, 3588, 4039, 3902, 4082, 3994, 3951, 4279, 3859, 3903, 3986, 3879, 3945 */
                            },
                            dataLabels = new DataLabels {
                                enabled = true,
                                rotation = -90,
                                color = "#FFFFFF",
                                align = "right",
                                y = 10
                            }
                        },
                        new Serie
                        {
                            name = "Executadas",
                            data = new int[]
                            { 3757, 3880, 3588, 4039, 3902, 4082, 3994, 3951, 4279, 3859, 3903, 3986, 3879, 3900 },
                                dataLabels = new DataLabels {
                                    enabled = true,
                                    rotation = -90,
                                    color = "#FFFFFF",
                                    align = "right",
                                    y = 10
                                }
                        }
                    }
                };

series对象将在其data参数而不是numbers数组中接收查询,但是我似乎找不到找到将我的查询引用到该位置的方法。

所以我真正需要的是用我发送的data = 3757, 3880, 3588, 4039, 3902, 4082, 3994, 3951, 4279, 3859, 3903, 3986, 3879, 3945},的静态数据代替我的obj[i] = temp,后者接收sql查询。

1 个答案:

答案 0 :(得分:0)

我假设您正在使用C#Asp.net Framework Web API。

public class MPreventivasAXEController : ApiController
{

    Grafico grafico = new Grafico
    {
        categories = new string[] {
        "Média 2016"
        , "Jan"
        , "Fev"
        , "Mar"
        , "Abril"
        , "Maio"
        , "Jun"
        , "Jul"
        , "Ago"
        , "Set"
        , "Out"
        , "Nov"
        , "Dez"
        , "Média 2017"
        },
        series = new Serie[] {
            new Serie
            {
                name = "Abertas",
                data = new int[] {},
                dataLabels = new DataLabels {
                    enabled = true,
                    rotation = -90,
                    color = "#FFFFFF",
                    align = "right",
                    y = 10
                }
            },
            new Serie
            {
                name = "Executadas",
                data = new int[] {},
                dataLabels = new DataLabels {
                    enabled = true,
                    rotation = -90,
                    color = "#FFFFFF",
                    align = "right",
                    y = 10
                }
            }
        }
    };

    public static void createCommand()
    {
        ConnectionFactory db = new ConnectionFactory();
        db.abrirConexao();

        SqlCommand cmd = new SqlCommand();

        string sqlAbertas = "SELECT COUNT(*) AS mes" +
                            "FROM WorkOrder WHERE workOrderSource = '02'" +
                            "AND YEAR(workOrderDate)=2018" +
                            "AND conclusionDate IS NULL" +
                            "GROUP BY MONTH(workOrderDate)";
        cmd.Connection = db.conn;
        cmd.CommandText = sqlAbertas;


        SqlDataReader res = cmd.ExecuteReader();
        int i = 0;
        List<int> obj = new List<int>();
        while (res.Read())
        {
            int temp;

            int.TryParse(res["mes"].ToString(), out temp);
            obj.Add( tmp )
        }

        grafico.series[0].data = obj.ToArray();
    }
}

这是一个简单的例子,我认为您需要带来更多结果进行比较。我已经将数据添加到该系列的第一个索引中。

也许您应该使用列表类型。当您使用另一种编程语言时,它更易于处理,学习起来也更少。您可以像我一样将其解析/转换为数组。

本文可以为您提供有关列表的帮助: https://www.learncs.org/en/Lists