與加密后的Access數據庫建立連接
Default.aspx<%@ 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 id="Head1" runat="server"><title>與加密后的Access數據庫建立連接</title></head><body><form id="form1" runat="server"><div><div><table style="width: 587px; height: 338px"><tr><td colspan="6"><asp:Image ID="Image1" runat="server" ImageUrl="~/image/head.gif" /></td></tr><tr><td style="width: 100px; height: 13px"><asp:Label ID="Label1" runat="server" Font-Size="Smaller" Text="輸入連接Access數據庫的密碼:"Width="199px"></asp:Label></td><td style="width: 77px; height: 13px"><asp:TextBox ID="TxtMm" runat="server" TextMode="Password"></asp:TextBox></td><td style="width: 77px; height: 13px"><asp:Button ID="BtnOK" runat="server" OnClick="BtnOK_Click" Text="顯示數據" Width="58px" /></td><td style="width: 100px; height: 13px"><asp:Label ID="lblMessage" runat="server" Font-Bold="True" Font-Size="Smaller" ForeColor="Red"Width="296px"></asp:Label></td><td style="width: 100px; height: 13px"></td><td style="width: 95px; height: 13px"></td></tr><tr><td colspan="6" style="height: 142px"><asp:GridView ID="GridbookSell" runat="server" AutoGenerateColumns="False" CellPadding="4"Font-Size="Smaller" ForeColor="#333333" GridLines="None" Width="788px"><FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><Columns><asp:BoundField DataField="圖書編號" HeaderText="圖書編號" /><asp:BoundField DataField="圖書名稱" HeaderText="圖書名稱" /><asp:BoundField DataField="價格" HeaderText="價格" /><asp:BoundField DataField="出版時間" HeaderText="出版時間" /><asp:BoundField DataField="類別" HeaderText="類別" /><asp:BoundField DataField="備注" HeaderText="備注" /></Columns><RowStyle BackColor="#F7F6F3" ForeColor="#333333" /><EditRowStyle BackColor="#999999" /><SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /><PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /><HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><AlternatingRowStyle BackColor="White" ForeColor="#284775" /></asp:GridView></td></tr><tr><td colspan="6" style="height: 60px"><asp:Image ID="Image2" runat="server" ImageUrl="~/image/foot.gif" /></td></tr></table></div></div></form></body></html>
Default.aspx.csusing 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.OleDb;public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void BtnOK_Click(object sender, EventArgs e){string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=" + this.TxtMm.Text + ";User Id=admin;Data source=" + Server.MapPath("db_ADO.mdb");OleDbConnection Con = new OleDbConnection(ConStr);if (this.TxtMm.Text == ""){Response.Write("<script language=javascript>alert('對不起!文本框不能為空!');location='javascript:history.go(-1)'</script>");}else{try{//打開數據庫連接Con.Open();OleDbDataAdapter Dap = new OleDbDataAdapter("select * from tb_booksell", Con);DataSet ds = new DataSet();Dap.Fill(ds, "tb_booksell");GridbookSell.DataSource = ds;GridbookSell.DataBind();this.lblMessage.Text = " 恭喜你,與加密后的Access數據庫連接成功!";}catch (Exception error){this.lblMessage.Text = " 很遺憾,密碼錯誤,請重新輸入密碼!";return;}finally{//關閉數據庫連接Con.Close();}}}}
<%@ 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 id="Head1" runat="server"> <title>與加密后的Access數據庫建立連接</title> </head> <body> <form id="form1" runat="server"> <div> <div> <table style="width: 587px; height: 338px"> <tr> <td colspan="6"> <asp:Image ID="Image1" runat="server" ImageUrl="~/image/head.gif" /></td> </tr> <tr> <td style="width: 100px; height: 13px"> <asp:Label ID="Label1" runat="server" Font-Size="Smaller" Text="輸入連接Access數據庫的密碼:" Width="199px"></asp:Label></td> <td style="width: 77px; height: 13px"> <asp:TextBox ID="TxtMm" runat="server" TextMode="Password"></asp:TextBox></td> <td style="width: 77px; height: 13px"> <asp:Button ID="BtnOK" runat="server" OnClick="BtnOK_Click" Text="顯示數據" Width="58px" /></td> <td style="width: 100px; height: 13px"> <asp:Label ID="lblMessage" runat="server" Font-Bold="True" Font-Size="Smaller" ForeColor="Red" Width="296px"></asp:Label></td> <td style="width: 100px; height: 13px"> </td> <td style="width: 95px; height: 13px"> </td> </tr> <tr> <td colspan="6" style="height: 142px"> <asp:GridView ID="GridbookSell" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Size="Smaller" ForeColor="#333333" GridLines="None" Width="788px"> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="圖書編號" HeaderText="圖書編號" /> <asp:BoundField DataField="圖書名稱" HeaderText="圖書名稱" /> <asp:BoundField DataField="價格" HeaderText="價格" /> <asp:BoundField DataField="出版時間" HeaderText="出版時間" /> <asp:BoundField DataField="類別" HeaderText="類別" /> <asp:BoundField DataField="備注" HeaderText="備注" /> </Columns> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </td> </tr> <tr> <td colspan="6" style="height: 60px"> <asp:Image ID="Image2" runat="server" ImageUrl="~/image/foot.gif" /></td> </tr> </table> </div> </div> </form> </body> </html> |
Default.aspx.cs
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.OleDb; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnOK_Click(object sender, EventArgs e) { string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=" + this.TxtMm.Text + ";User Id=admin;Data source=" + Server.MapPath("db_ADO.mdb"); OleDbConnection Con = new OleDbConnection(ConStr); if (this.TxtMm.Text == "") { Response.Write("<script language=javascript>alert('對不起!文本框不能為空!');location='javascript:history.go(-1)'</script>"); } else { try { //打開數據庫連接 Con.Open(); OleDbDataAdapter Dap = new OleDbDataAdapter("select * from tb_booksell", Con); DataSet ds = new DataSet(); Dap.Fill(ds, "tb_booksell"); GridbookSell.DataSource = ds; GridbookSell.DataBind(); this.lblMessage.Text = " 恭喜你,與加密后的Access數據庫連接成功!"; } catch (Exception error) { this.lblMessage.Text = " 很遺憾,密碼錯誤,請重新輸入密碼!"; return; } finally { //關閉數據庫連接 Con.Close(); } } } } |
posted on 2014-07-15 10:30 順其自然EVO 閱讀(181) 評論(0) 編輯 收藏 所屬分類: 數據庫