今天總結下ASP.NET中的基本資料通路。
寫過ASP資料庫程式設計的朋友應該知道,在ASP中通路資料庫主要用到三大對象:
Connection, Command, RecordSet
新一代的ADO.NET對老的ADO進行了更新,主要有四大對象:
1)SqlConnection
2)SqlCommand
3)SqlDataAdapter
4)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;
}
}