如何从数据库添加数据并将其设置为高图C#中的数据系列

时间:2014-04-23 17:33:10

标签: c# .net database sql-server-2008 highcharts

我是c#

的新手

我想从数据库设置数据,但我有一些问题。在高图中,Serie.Data是对象数组。现在要从数据库中获取数据并将其保持为列表,然后我尝试将列表转换为object [],但它仍然是错误,无法将列表转换为对象[]。你能说出从数据库设置数据的最佳方法是什么,并将其设置为高图。

这是我的代码

namespace Please
{
public partial class plotLayout : System.Web.UI.Page
{

    int x=0;

    protected void Page_Load(object sender, EventArgs e){
        //variable
        SqlCommand cmd;
        SqlDataReader data_read;



        //connect sql server
        SqlConnection sqlCon = new SqlConnection(@"Data Source=SUPERTONG-PC\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True");
        //create array list to keep data from data_read

        List<Double> costs = new List<Double>();
        List<Double> revenues = new List<Double>();
        //open sql
        try
        {
            sqlCon.Open();
            Response.Write("connection is successfull");
            //get value in Table emploSalary
            //crate adapter to sent your string direction sql with db database server
            //SqlDataAdapter AdapSql = new SqlDataAdapter("select salary from emploSalary", sqlCon);
           String getSalary = "select salary from emploSalary";
           cmd = new SqlCommand(getSalary, sqlCon);
           data_read = cmd.ExecuteReader();
            //function sqldata.read จะทำการ อ่าน ข้อมูลให้ที่เดียว 
           while(data_read.Read()) {

               //Response.Write(data_read["fname"]);

               //add salary to list
               costs.Add(Convert.ToDouble(data_read["salary"]));

           }

           data_read.Close();

           Response.Write("num list ::" + costs.Count());

           // get data in table revenue 
           String getRevenue = "select revenue from comRevennue";
           cmd = new SqlCommand(getRevenue, sqlCon);
           data_read = cmd.ExecuteReader();
           while (data_read.Read()) {

               revenues.Add(Convert.ToDouble(data_read["revenue"]));

           }



           Response.Write("cost ::" + costs.Count());
           Response.Write("revenue ::" + revenues.Count());


           data_read.Close();
        }
        catch(Exception ex) {
            Response.Write("Error exception ::" + ex.Message.ToString());
        }



        //create title on Y && under X

        hcLine.Title = new Title("Main Title");
        hcLine.SubTitle = new SubTitle("Sub Title");
        hcLine.Theme = "gray";
        hcLine.Legend = new Legend{align = Align.center };

        //set main YAxis to left hand side.
        hcLine.YAxis.Add(new YAxisItem { title = new Title("SuperSup Title")});
        hcLine.YAxis.Add(new YAxisItem { title = new Title("SuperSub Title"), opposite = true });

        //hcLine.YAxis.Add(new YAxisItem { title = new Title("Faturamento") });

        hcLine.XAxis.Add(new XAxisItem {categories = new []{"Jan", "Feb", "Mar", "Apr", "May"}});

        var test_series = new List<Serie>();

        // can not cast list to object[]
        test_series.Add(new Serie { data = revenues.Cast<object[]>().ToArray(), name = "year1991", yAxis = 0 });

        test_series.Add(new Serie { data = new object[] { 60.6, 49.7}, name = "year1992", yAxis = 1 });


        //data to plot
        //var serise = new Collection<Serie>();
        //serise.Add(new Serie {data = new object[] {58.3, 76.2, 24.8, 88.2, 150.3}});
        //plot options
        hcLine.PlotOptions = new PlotOptionsColumn { dataLabels = new DataLabels { enabled = true }};
        //set data to grap line
        hcLine.DataSource = test_series;
        hcLine.DataBind();

    }
}
}

谢谢

1 个答案:

答案 0 :(得分:0)

试试这个......(从Cast<object[]>删除[])

test_series.Add(new Serie { data = revenues.Cast<object>().ToArray(), name = "year1991", yAxis = 0 });

希望这有帮助。