0
我正在研究數據訪問層和存儲過程。我創建了一個「提取」類來從數據庫表「car」中檢索數據並將其顯示在HTML表格中。爲此,我製作了一個名爲「抓取」的方法。在這裏,我使用ArrayList,但它不工作。當我運行我的應用程序時,它會在HTML表的每一列中顯示以下內容。如何使用asp.net中的類將數據從數據庫顯示到HTML表格中?
'System.Collections.ArrayList'
這是我的存儲過程:
create proc sp_experiment
as
select Id, car name, engine number,nameplate from car
這是我的班 「取」,它有一個名爲方法 「獲取」
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
public class fetch
{
private static SqlCommand cmd;
private static SqlDataReader sdr;
private static ArrayList ht;
public static ArrayList fetching()
{
using (cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_experiment";
cmd.Connection = getconnected.getconnection();
using (sdr = null)
{
using (sdr = cmd.ExecuteReader())
{
ht = new ArrayList();
while (sdr.Read())
{
ht.Add(sdr[0].ToString());
ht.Add(sdr[1].ToString());
ht.Add(sdr[2].ToString());
ht.Add(sdr[3].ToString());
}
}
return ht;
}
}
}
這是我的HTML表格代碼:
<form id="formlist" runat="server">
<table class="col-lg-12 col-md-12">
<tr>
<td>
<table id="itemPlaceholderContainer" runat="server" class="table table-striped table-bordered table-hover">
<tr>
<th> ID</th>
<th>Car Name</th>
<th>Engine Number</th>
<th>Name Plate</th>
</tr>
<tr runat="server" id="exp">
<td runat="server" id="carid"></td>
<td runat="server" id="name"></td>
<td runat="server" id="carengineno"></td>
<td runat="server" id="carnameplate"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
這是後端我的HTML表的代碼:
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.Adapters;
public partial class carlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
carid.InnerText = fetch.fetching().ToString();
name.InnerText = fetch.fetching().ToString();
carengineno.InnerText = fetch.fetching().ToString();
carnameplate.InnerText = fetch.fetching().ToString();
}
}
,我建議你應該閱讀的編碼/命名約定爲C#。 –