2011-09-21 134 views
0
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语句来调用项目列表并存储在上述数组中。我这样做是因为之前数组中的项目是硬编码的。现在我想从数据库中检索它,因为每当有新项目时,都会自动绘制一个新的饼图。如何在数组中存储数据库中的项目?

+0

哪些** **项目要存储到一个数组数据库来做到这一点? – adatapost

+0

你的问题是什么? – Icarus

+0

为什么将它存储在一个数组中,只是将它存储为List <>并捕获数据行 – CBRRacer

回答

1

这里的方式与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 
相关问题