嗨,我正在尝试从数据库中读取数据,然后将其放入表中 . 表中的行是根据数据库中的行数自动创建的 . 我在php中使用mysql_fetch_array做了这个,但似乎无法在asp.net webforms中做到这一点 . 我的想法是使用此查询获取信息并存储在服务器页面中的标签中,并使用标签填充coloumns数据创建一个表 . 这是我在'page_load'中的代码谢谢:
<table>
<tr>
<th>Surgery</th>
<th>PatientID</th>
<th>Location</th>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label3" runat="server"></asp:Label>
</td>
</tr>
</table>
string query= "select surgery,patientID, location from details";
SqlCommand result= new SqlCommand(query, conn);
result.ExecuteNonQuery();
using (SqlDataReader getInfo= result.ExecuteReader())
while (getInfo.Read())
{
Label1.Text = getInfo["surgery"].ToString();
Label2.Text = getInfo["patientID"].ToString();
Label3.Text = getInfo["location"].ToString();
}
SqlCommand cmd = new SqlCommand(“select surgery,PatientID,location from details”,conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(DT); GridView1.DataSource = dt; GridView1.DataBind(); conn.Close();
1 回答
在
aspx
代码中添加GridView
:您可以使用C#代码,只需在绑定到gridview之前关闭连接: