Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>强类型DataSet与SqlDataAdapter搭配查询</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="gvInfo" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AllowPaging="True" onpageindexchanging="gvInfo_PageIndexChanging" PageSize="5"> <FooterStyle BackColor="Tan" /> <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <AlternatingRowStyle BackColor="PaleGoldenrod" /> </asp:GridView> </form> </body> </html>
Default.aspx.cs
View Code
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } public void BindData() { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); //创建数据连接 conn.Open(); SqlCommand cmd = new SqlCommand("select * from tb_inf", conn); //创建数据命令 SqlDataAdapter adapter = new SqlDataAdapter(); //创建SqlDataAdapter adapter.SelectCommand = cmd; //Adapter指定使用SqlCommand dsInfo ds = new dsInfo(); //创建强类型DataSet数据集,以容纳数据 adapter.Fill(ds.tb_inf); //加入数据到DataSet之中 gvInfo.DataSource = ds.tb_inf; //指定GridView控件为tb_info gvInfo.DataBind(); //数据绑定 } protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvInfo.PageIndex = e.NewPageIndex; BindData(); } }