簡體   English   中英

如何從數組中存儲數據庫中的項目?

[英]how to store items from database inside an array?

public partial class Piechart : System.Web.UI.Page
{
    private decimal total = 0; // course total
    private decimal registered = 0;
    private decimal regAttend = 0;
    private decimal nRegAttend = 0;
    private int regPer = 0;
    private int regToTotalPer = 0;
    private int nRegPer = 0;
    private int angle = 0;
    private int angle2 = 0;
    private int angle3 = 0;
    private int parTotal = 0;
    //const string G = "SELECT DISTINCT COUNT(Grouping) from Attendance";
   // SqlConnection c = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
    //SqlCommand Command = new SqlCommand(G);


    private string[] Status = new string[2] { "Attend", "Didn't Attend" };
    private string[] Course = new string[] {//items from database};

我想使用sql語句調用項目列表並存儲在上述數組中。 我這樣做是因為以前數組中的項目都是硬編碼的。 現在,我想從數據庫中檢索它,因為只要有新項目,就會自動繪制一個新的餅圖。

這是使用ArrayList做到這一點的方法

ArrayList Course = new ArrayList();
const string query = "SELECT DISTINCT COUNT(Grouping) from Attendance";
const string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true";
using (SqlConnection cn = new SqlConnection(connectionString))
{
    using (SqlCommand cm = new SqlCommand(query, cn))
    {
        cn.Open();
        SqlDataReader reader = cm.ExecuteReader();
        while (reader.Read())
        {
            Course.Add(reader.GetString(0));
        }
    }
}

//Course.ToArray(); // <-- cast to string array object do use in the pie chart

暫無
暫無

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

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