0

如何从 sql server 2008 检索所有数据到我在 asp.net 中的页面这是 aspx 代码:

    <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" />
    <asp:Label ID="LabelName" runat="server" Text=""></asp:Label><br />
    <asp:Label ID="LabelAddress" runat="server" Text="Label"></asp:Label>

这是我的 aspx.cs 代码:

    string cs=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con=new SqlConnection(cs))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);

            SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    ImageProfile.ImageUrl=dr["image"].ToString();
                    LabelName.Text=dr["Name"].ToString();
                    LabelAddress.Text=dr["Address"].ToString();

                }

        }

但我的输出只有一个结果,而我有超过三列感谢任何指导

4

3 回答 3

1

试试这个方法

<asp:Repeater ID="rpt" runat="server">
    <ItemTemplate>
        <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" ImageUrl = "<% # Eval("image") %>" />
        <asp:Label ID="LabelName" runat="server" Text = "<% # Eval("Name") %>"  ></asp:Label>
        <asp:Label ID="LabelAddress" runat="server" Text="<% # Eval("Address") %>"></asp:Label>
    </ItemTemplate>
</asp:Repeater>

在您的 C# 代码中编写这只是将数据源设置为转发器控件

于 2013-09-10T11:05:49.380 回答
0

尝试这个

    <asp:Repeater ID="RepDetails" runat="server">
      <ItemTemplate>
           <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" ImageUrl = "<% # Eval("image") %>" />
           <asp:Label ID="LabelName" runat="server" Text = "<% # Eval("Name") %>"  ></asp:Label>
           <asp:Label ID="LabelAddress" runat="server" Text="<% # Eval("Address") %>"></asp:Label>
      </ItemTemplate>
    </asp:Repeater>

    string cs=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con=new SqlConnection(cs))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);


       DataSet ds = new DataSet();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(ds);
       RepDetails.DataSource = ds;
       RepDetails.DataBind();
    }
于 2013-09-10T11:16:43.593 回答
0

最好将您的标签和图像控件放置在 gridview 模板中......这样所有东西都会占据它......

SqlConnection con=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
ImageProfile.ImageUrl=ds.Tables[0].Rows[j][["image"].ToString();
LabelName.Text=ds.Tables[0].Rows[j]["Name"].ToString();
LabelAddress.Text=ds.Tables[0].Rows[j]["Address"].ToString();
}
于 2013-09-10T11:14:14.250 回答