簡體   English   中英

C#:使用 SqlDataReader 在圖表上顯示 SQL 查詢

[英]C# : display SQL query on a chart using SqlDataReader

這是我的查詢,我想像這樣顯示它:命令日期的命令總和(每個日期應該有一個欄顯示該特定日期(天)有多少銷售(命令)

select 
    date_comm as 'jourdetravail',
    sum(TotalCom) as 'recettedujour' 
from 
    Commande_art 
group by 
    date_comm

在此處輸入圖像描述

您可以使用此腳本填充數據表,然后將數據表用作圖表的源,並且您需要在 x 軸上對圖表進行索引

                SqlConnection con = new SqlConnection("Data Source=<Your Server>;Initial Catalog=<YourDatabase>;Persist Security Info=True;User ID=<yourID>;Password=<yourPassword>");
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select [date_comm] as 'jourdetravail', sum([TotalCom]) as 'recettedujour' from [dbo].[Commande_art] group by [date_comm]";
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

然后是你的“dt”:

<yourChart>.DataSource = dt;

您還需要使用與查詢列相同的名稱設置圖表的 2 個系列,以便正確顯示它們

希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM