ASP Connect Database Login button

Source: Internet
Author: User
Tags connectionstrings

(1) Add the following code to the configuration file:

<connectionStrings>
<add name= "Accessconn" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=| Datadirectory|\film.mdb; " />
<add name= "sqlconn" connectionstring= "Data source=localhost;initial catalog=db_aspnet; Trusted_connection=sspi; " />
</connectionStrings>

This is the first step to complete the work.

(2) Introducing the node file namespace in the code file

Using System.Configuration;

(3) Edit the database connection code in the button event

protected void Button1_Click (object sender, EventArgs e)//Login button event code, with access as an example, SQL and so on
{
String str1 = configurationmanager.connectionstrings["Accessconn"]. Connectionstring.tostring (); The node that reads the configuration file
String str2 = configurationmanager.connectionstrings["Sqlconn"]. Connectionstring.tostring ();
OleDbConnection connstr1 = new OleDbConnection (STR1); Database Connection Object
SqlConnection connstr2 = new SqlConnection (STR2);
if (connstr1. state = = System.Data.ConnectionState.Closed)//Open Database
{
Connstr1. Open ();
}
String content = "SELECT * from User_info where uname= '" +username. Text+ "' and upwd= '" +userpwd. text+ "'"; Writing SQL statements
OleDbCommand commstr1 = new OleDbCommand (content, CONNSTR1);//Execute SQL statement
SqlCommand commstr2 = new SqlCommand (content, CONNSTR2);
OleDbDataReader dr = Commstr1. ExecuteReader ();//Accept Query Results
SqlDataReader dr= commstr2. ExecuteReader ();
if (Dr. Read ())//Determine if the query results Dr has data
{
Response.Redirect ("welcome.aspx");//Jump page

Response.Write ("<script>alert (' successful landing '); location.href= ' welcome.aspx ';</script>");
}
Else
{
Response.Write ("<script>alert (' username or password is wrong! ') </script> ");
}
Dr. Close ();
if (connstr1. state = = System.Data.ConnectionState.Open)//Close Database
{
Connstr1. Close ();
}
}

ASP Connect Database Login button

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.