Default.aspx
View Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml" > 6 <head id="Head1" runat="server"> 7 <title>建立Access数据库连接</title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 <div> 13 <div style="text-align: center"> 14 <table style="width: 779px"> 15 <tr> 16 <td colspan="8"> 17 <asp:Image ID="Image1" runat="server" ImageUrl="~/image/head.gif" /></td> 18 </tr> 19 <tr> 20 <td align="left" rowspan="11" style="width: 86px"> 21 <asp:Image ID="Image3" runat="server" ImageUrl="~/image/list.gif" /></td> 22 <td style="height: 1px" valign="top"> 23 <asp:Image ID="Image4" runat="server" ImageUrl="~/image/info.gif" /></td> 24 </tr> 25 <tr> 26 <td style="height: 1px" valign="top"> 27 <asp:Button ID="BtnOK" runat="server" OnClick="BtnOK_Click" Text="连接Access数据库显示信息" 28 Width="189px" /></td> 29 </tr> 30 <tr> 31 <td colspan="7" rowspan="5" valign="top"> 32 <asp:GridView ID="GrvTeacherInfo" runat="server" AllowPaging="True" AutoGenerateColumns="False" 33 CellPadding="4" Font-Size="Smaller" ForeColor="#333333" GridLines="None" Height="150px" 34 OnPageIndexChanging="GrvTeacherInfo_PageIndexChanging" PageSize="8" Width="593px"> 35 <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 36 <Columns> 37 <asp:BoundField DataField="姓名" HeaderText="姓名" /> 38 <asp:BoundField DataField="身份/学历" HeaderText="身份/学历" /> 39 <asp:BoundField DataField="可教授课程" HeaderText="可教授课程" /> 40 </Columns> 41 <RowStyle BackColor="#EFF3FB" /> 42 <EditRowStyle BackColor="#2461BF" /> 43 <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 44 <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 45 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 46 <AlternatingRowStyle BackColor="White" /> 47 </asp:GridView> 48 </td> 49 </tr> 50 <tr> 51 </tr> 52 <tr> 53 </tr> 54 <tr> 55 </tr> 56 <tr> 57 </tr> 58 <tr> 59 <td colspan="7" rowspan="4"> 60 </td> 61 </tr> 62 <tr> 63 </tr> 64 <tr> 65 </tr> 66 <tr> 67 </tr> 68 <tr> 69 <td colspan="8"> 70 <asp:Image ID="Image2" runat="server" ImageUrl="~/image/foot.gif" /></td> 71 </tr> 72 </table> 73 </div> 74 </div> 75 76 </div> 77 </form> 78 </body> 79 </html>
Default.aspx.cs
View Code
1 using System; 2 using System.Configuration; 3 using System.Data; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 using System.Xml.Linq; 12 13 using System.Data.OleDb; 14 public partial class _Default : System.Web.UI.Page 15 { 16 protected void Page_Load(object sender, EventArgs e) 17 { 18 } 19 public void bindGridView() 20 { 21 string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=|DataDirectory|db_ADO.mdb;"; 22 OleDbConnection Con = new OleDbConnection(ConStr); 23 Con.Open(); 24 OleDbDataAdapter OleDbdap = new OleDbDataAdapter("select * from tb_TeacherInfo", Con); 25 DataSet ds = new DataSet(); 26 OleDbdap.Fill(ds, "tb_TeacherInfo"); 27 this.GrvTeacherInfo.DataSource = ds; 28 this.GrvTeacherInfo.DataBind(); 29 Con.Close(); 30 } 31 protected void GrvTeacherInfo_PageIndexChanging(object sender, GridViewPageEventArgs e) 32 { 33 this.GrvTeacherInfo.PageIndex = e.NewPageIndex; 34 this.bindGridView(); 35 } 36 protected void BtnOK_Click(object sender, EventArgs e) 37 { 38 try 39 { 40 this.bindGridView(); 41 Response.Write("<script language=javascript>alert('连接Access数据库成功!');</script>"); 42 43 } 44 catch 45 { 46 Response.Write("<script language=javascript>alert('连接Access数据库失败!');</script>"); 47 } 48 49 } 50 }