我在输出到 WebGrid 时遇到问题,因为我的列表被覆盖了,所以到最后,我为网格上的每一行写入了最后一行数据。我必须使用 while 循环,因为数据不断被添加到,我们正在查看大量数据,所以我试图不写入另一个列表。
public class ChemData
{
string strSQLconnection = "Server=Server;Database=data;Uid=Username;Pwd=Password";
public int productId { get; set; }
public string productName { get; set; }
public List<ProdData> ProdList = new List<ProdData>();
public List<ProdData> ProdDataPull()
{
ProdData Analysis = new ProdData();
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(strSQLconnection);
SqlCommand query = new SqlCommand("Select * from producttable");
conn.Open();
query.Connection = new SqlConnection(strSQLconnection);
query.Connection.Open();
reader = query.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0)) Analysis.productId = reader.GetInt32(0);
if (!reader.IsDBNull(1)) Analysis.productName = reader.GetString(1);
ProdList.Add(Analysis);
}
return ChemList;
}
}