mysql_fetch_array 在 asp.net 网络表单中

mysql_fetch_array in asp.net webforms

您好,我正在尝试从数据库中读取数据,然后将其放入 table。 table 中的行是根据数据库中的行数自动创建的。我在 php 中使用 mysql_fetch_array 完成了此操作,但似乎无法在 asp.net 网络表单中完成。我的想法是使用这个查询来获取信息并存储在服务器页面的标签中,并在那里创建一个 table 并使用标签填充列数据。这是我在 '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();

在您的 aspx 代码中添加 GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="Surgery" DataField="surgery" />
        <asp:BoundField HeaderText="PatientID" DataField="patientID" />
        <asp:BoundField HeaderText="Location" DataField="location" />
    </Columns>
</asp:GridView>

你的C#代码没问题,在绑定到gridview之前关闭连接即可:

SqlCommand cmd = new SqlCommand("select surgery, PatientID, location from details", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); 
sda.Fill(dt);

conn.Close();

GridView1.DataSource = dt; 
GridView1.DataBind();