Asp. NET connection Access database methods _ Practical Tips

Source: Internet
Author: User

Web.config

The first method:

<?xml version= "1.0" encoding= "Utf-8"?> <!--for more information about how to configure the ASP.net application, please visit http://go.microsoft.com/fwlink/? linkid=169433--> <configuration> <appSettings> <add key= "accessconnstring" value= "provider=" Microsoft.jet.oledb.4.0;data source= "/> <add key=" Accessdbpath "value=" ~/app_data/guestbook.mdb "/>" appsettings> <connectionStrings> <add name= "accessconnectionstring" connectionstring= "provider=" Microsoft.jet.oledb.4.0;data source= "providername=" System.Data.OleDb/> <add name= "Access_path" connectionstring= "~/app_data/guestbook.mdb"/> <add name= "sqlConnectionString" Data connectionString= Localhost;initial Catalog=guestbook; User id=sa;password=123456; "providername=" System.Data.SqlClient "/> <add name=" oracleconnectionstring " Connectionstring= "Data source= (description= address_list= (address= (protocol=tcp) (host=xp2008) (PORT=1521)) ( Connect_data= (SID=GUANDB) (server=dedicated)); User Id=system; password=k2763826; Unicode=true "providername=" System.Data.OracleClient "/> </connectionStrings> <system.web> <compilation Debug= "true" targetframework= "4.0"/> </system.web> </configuration>

The second method:

<?xml version= "1.0" encoding= "Utf-8"?>
<!--
For more information about how to configure the ASP.net application, visit the
http:// go.microsoft.com/fwlink/? linkid=169433
-->
<configuration>
<appSettings>
<add key= "Accessconnstring" Value= "Provider=Microsoft.Jet.OLEDB.4.0;Data source="/> <add key= "Accessdbpath" value=
Guestbook.mdb "/>
</appSettings>
<connectionStrings>
<add name=" Accessconnectionstring "connectionstring=" Provider=Microsoft.Jet.OLEDB.4.0;Data source= "/> <add
" Access_path "connectionstring=" ~/app_data/guestbook.mdb "/> <add name=" sqlConnectionString "
connectionstring= "Data source=localhost;initial catalog=huarundb; User id=sa;password=123456; "providername=" System.Data.SqlClient "/>
</connectionStrings>
< system.web>
<compilation debug= "true" targetframework= "4.0"/>
</system.web>
</ Configuration>

background Aspx.CS

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.Configuration;
Using System.Configuration;
Using System.Data.OleDb;
Using System.Data; namespace Testoracle {public partial class TestConnect:System.Web.UI.Page {public static readonly string connStr1 = Sy Stem. web.configuration.webconfigurationmanager.connectionstrings["Accessconnectionstring"]. connectionstring+ HttpContext.Current.Server.MapPath (webconfigurationmanager.connectionstrings["Access_path").
ConnectionString); public static readonly String connStr2 = system.configuration.configurationmanager.appsettings["Accessconnstring".
ToString () + System.Web.HttpContext.Current.Server.MapPath (configurationmanager.appsettings["Accessdbpath"]) + ";"; public static readonly String CONNSTR3 = "Provider = microsoft.jet.oledb.4.0;D ata source=" + httpcontext.current.server.m
Appath ("~/app_data/guestbook.mdb"); protected void Page_Load (Object sender, EventArgs e) {} protected void Button1_Click (object sender, EventArgs e) {OleDbConnection conn = new OleD
Bconnection (CONNSTR1); try {Conn.
Open ();
String sql = "SELECT * from Gtcontent";
OleDbDataAdapter myadapter = new OleDbDataAdapter (SQL, conn);
DataSet ds = new DataSet (); Myadapter.
Fill (DS); This.
Gridview1.datasource = ds; This.
Gridview1.databind (); This. Label1.Text = "Database connection Successful!"
";
} catch (Exception ee) {this. Label1.Text = ee.
ToString (); }
}
}
}

The above is a small set of asp.net to the introduction of the Access database of several methods, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.