一个asp.net图表中的C#2数据集

时间:2014-04-16 11:06:27

标签: asp.net sql charts dataset

我有两个表格,我想在一个图表上显示,StockIssued和StockOrdered。我有SELECT语句,显示两个表每年的每月费用

SELECT YEAR(IssueDate) AS 'Year', MONTH(IssueDate) AS 'Month', SUM(TotalCost) AS 'TotalCostIssue'
FROM IssueStock
GROUP BY YEAR(IssueDate), MONTH(IssueDate)
order by 'Year', 'Month'  

SELECT YEAR(order_date) AS 'Year', MONTH(order_date) AS 'Month', SUM(cost) AS 'TotalCostOrder'
FROM Orders
GROUP BY YEAR(order_date), MONTH(order_date)
order by 'Year', 'Month'

发行股票
年份总费用
2010 --- 1 --- 1500
2010 --- 2 --- 1400
..........

库存订购
年份总费用
2010 --- 1 --- 1600
2010 --- 2 --- 1700
............

如何在同一图表上一起显示每个月的这些费用?

C#代码

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);

        string SelectQuery1 = "SELECT YEAR(IssueDate) AS 'Year', MONTH(IssueDate) AS 'Month', SUM(TotalCost) AS 'TotalCost' " +
            "FROM IssueStock WHERE YEAR(IssueDate) = " + year + " GROUP BY YEAR(IssueDate), MONTH(IssueDate) order by 'Year', 'Month'";

        SqlCommand myCommand = new SqlCommand(SelectQuery1, conn);

        myCommand.Connection.Open();

        SqlDataAdapter myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = myCommand;

        DataSet myDataSet = new DataSet();

        myDataAdapter.Fill(myDataSet, "Query");

        Chart1.DataSource = myDataSet;
        Chart1.Series["Series1"].XValueMember = "Month";
        Chart1.Series["Series1"].YValueMembers = "TotalCostIssue";

        Chart1.DataBind();

        myCommand.Connection.Close();

1 个答案:

答案 0 :(得分:0)

由于您的两个表具有相同的结构。您还可以在UNION ALL命令中使用SQL子句。这不是最好的解决方案,但你会得到结果。

string SelectQuery1 = "SELECT YEAR(IssueDate) AS 'Year', MONTH(IssueDate) AS 'Month', SUM(TotalCost) AS 'TotalCost' " +
        "FROM IssueStock  WHERE YEAR(IssueDate) = " + year ;
SelectQuery1 += " UNION ALL SELECT YEAR(IssueDate) AS 'Year', MONTH(IssueDate) AS 'Month', SUM(TotalCost) AS 'TotalCost' " +
        "FROM Orders WHERE YEAR(IssueDate) = " + year + " GROUP BY YEAR(IssueDate), MONTH(IssueDate) order by 'Year', 'Month'";