今天总结下ASP.NET中的基本数据访问。 写过ASP数据库编程的朋友应该知道,在ASP中访问数据库主要用到三大对象: Connection, Command, RecordSet新一代的ADO.NET对老的ADO进行了升级,主要有四大对象: 1)SqlConnection2)SqlCommand3)SqlDataAdapter4)DataSet 其中,SqlDataAdapter是新增加的 适配器对象。 它用来 填充结果集。 1)建立并打开连接2)根据连接和sql语句创建适配器3)用适配器填充结果集4)数据绑定-将结果 集绑定到控件 以北风数据库为例,具体来举个例子: ASPX代码:
<% @ Page Language = " C# " AutoEventWireup = " true " CodeBehind = " dataAccess1.aspx.cs " Inherits = " BlogNet.ASPXDemo.dataAccess1 " %> <! 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 > ASP.NET数据访问-四大对象 </ title > </ head > < body > < form id ="form1" runat ="server" > < div > < asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" AllowPaging ="True" AllowSorting ="True" PageSize ="20" OnPageIndexChanging ="GridView1_PageIndexChanging" > < Columns > < asp:BoundField DataField ="CustomerID" HeaderText ="CustomerID" ReadOnly ="True" SortExpression ="CustomerID" NullDisplayText ="N/A" /> < asp:BoundField DataField ="CompanyName" HeaderText ="CompanyName" SortExpression ="CompanyName" NullDisplayText ="N/A" /> < asp:BoundField DataField ="ContactName" HeaderText ="ContactName" SortExpression ="ContactName" NullDisplayText ="N/A" /> < asp:BoundField DataField ="ContactTitle" HeaderText ="ContactTitle" SortExpression ="ContactTitle" NullDisplayText ="N/A" /> < asp:BoundField DataField ="Address" HeaderText ="Address" SortExpression ="Address" NullDisplayText ="N/A" /> < asp:BoundField DataField ="City" HeaderText ="City" SortExpression ="City" NullDisplayText ="N/A" /> < asp:BoundField DataField ="Region" HeaderText ="Region" SortExpression ="Region" NullDisplayText ="N/A" /> < asp:BoundField DataField ="PostalCode" HeaderText ="PostalCode" SortExpression ="PostalCode" NullDisplayText ="N/A" /> < asp:BoundField DataField ="Country" HeaderText ="Country" SortExpression ="Country" NullDisplayText ="N/A" /> < asp:BoundField DataField ="Phone" HeaderText ="Phone" SortExpression ="Phone" NullDisplayText ="N/A" /> < asp:BoundField DataField ="Fax" HeaderText ="Fax" SortExpression ="Fax" NullDisplayText ="N/A" /> </ Columns > </ asp:GridView > </ div > </ form > </ body > </ html >
cs代码: using System; using System.Collections; 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; namespace BlogNet.ASPXDemo{ public partial class dataAccess1 : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { string strConn = " Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True " ; SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sql = " select * from Customers " ; SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); conn.Close(); } protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); } }}