CS
string connectionString = "server=.\\SQLEXPRESS;database=BookStore;uid=sa;pwd=sa";
string selectStrin = "Select * from Books";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(selectStrin, conn);
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Response.Write(reader[0].ToString()+": "+ reader["Title"].ToString() + "<br />");
}
reader.Close();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(selectStrin, conn);
da.Fill(ds);
DataTable dt = new DataTable();
GridView1.DataSource = ds;
GridView1.DataBind();
DataList1.DataSource = ds;
DataList1.DataBind();
控件
HTML
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"Title") %>'></asp:Label>
=><asp:Label ID="Label4" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"id","WebForm1.aspx?id={0}") %>'>HyperLink</asp:HyperLink>
</ItemTemplate>
</asp:DataList>