我有数据(3 个变量)要从 ms 访问数据库(产品表)中获取并将其显示在 aspx 页面上。我设法只显示第一行数据。似乎问题出在 foreach 循环中的 aspx 页面代码中。我就是头晕眼花。
这是 ProductController 类:
public ActionResult Index()
{
string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\vindom\\Desktop\\DB2.accdb";
// connection string from database Properties
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd;
OleDbDataReader reader;
con.Open();
{
cmd = new OleDbCommand("SELECT * FROM product", con);
reader = cmd.ExecuteReader();
while(reader.Read())
{
ViewData["Prod_Type"] = reader.GetValue(0);
ViewData["Prod_N"] = reader.GetValue(1);
ViewData["Prod_d"] = reader.GetValue(2);
}
con.Close();
return View();
}
}
这是aspx代码:
<table>
<tr>
<th>Product Type</th>
<th>Product Number</th>
<th>Product Details</th>
<th></th>
</tr>
<%@foreach (var item in View)
{%>
<tr>
<td><%= Html.Encode(ViewData["Prod_Type"]) %></td>
<td><%= Html.Encode(ViewData["Prod_N"]) %></td>
<td><%= Html.Encode(ViewData["Prod_d"]) %></td>
</tr>
<%}%>
</table>
..和Product.cs
namespace Tok.Models
{
public class Product
{
public string Product_Type { get; set; }
public string Product_NUM { get; set; }
public string Product_desc { get; set; }
public ICollection<Process> Processes;
}
public class Process
{
public string ProcessId { get; set; }
public string Process_desc { get; set; }
public string Next_processs { get; set; }
public virtual Product Product { get; set; }
}
}