ASP. NET connect Access database for login function

Source: Internet
Author: User
Tags connectionstrings

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">&nbsp;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.