(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