There's not much to talk about here, just demo the code.
Connecting to an Access database first requires the configuration of Web. config
<appSettings> <add key="accessconnstring"Value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="/> <add key="Accessdbpath"Value="~/app_data/news.mdb"/> </appSettings> <connectionStrings> <add name="accessconnectionstring"connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data source="/> <add name="Access_path"connectionstring="~/app_data/news.mdb"/> <add name="sqlConnectionString"connectionstring="Data source=localhost;initial catalog=huarundb; User Id=sa;password=zhuwenfan;"Providername="System.Data.SqlClient"/> </connectionStrings>
The first thing to do is to create a new App_Data folder in your root directory, move the database files to that folder, and then write the above code to the configuration file.
Front desk:
<Table> <TR><TD>User name:</TD><TD> <Asp:textboxID= "Textuser"runat= "Server"></Asp:textbox></TD></TR> <TR><TDclass= "Auto-style1"> Password:</TD><TDclass= "Auto-style1"> <Asp:textboxID= "TEXTPW"runat= "Server"></Asp:textbox></TD></TR> <TR> <TD></TD><TD><Asp:buttonID= "Button1"runat= "Server"Text= "Login"Width= "92px"OnClick= "Button1_Click" /></TD></TR> </Table>
Background:
Public Partial classLogin:System.Web.UI.Page { Public Static ReadOnly stringCONNSTR1 ="Provider = microsoft.jet.oledb.4.0;D ata source="+ HttpContext.Current.Server.MapPath ("~/app_data/news.mdb");//Connect Databaseprotected voidPage_Load (Objectsender, EventArgs e) { } protected voidButton1_Click (Objectsender, EventArgs e) {OleDbConnection connection=NewOleDbConnection (CONNSTR1); Connection. Open ();//Opening the databasestringsql ="select * FROM [User] where UserName = '"+ Textuser.text +"' and USERPW = '"+ Textpw.text +"'";//query which data matches the user name and password OleDbCommand command=NewOleDbCommand (SQL, connection); OleDbDataReader Reader=command. ExecuteReader (); if(reader. Read ())//If the match successfully reads the database contents {session["username"] =textuser.text;//Save the user name to the session Response.Redirect ("news.aspx"); Connection. Close ();//Shut Down Database Response.End (); } Else{Response.Write ("<script>alert (' username or password is wrong!! ') </script>");//otherwise Login failed}}}
This is only the simplest landing, for reference only, if there is any shortage of places can be proposed.
ASP. NET connect Access database for login function