Several Methods for Asp.net to connect to access

Source: Internet
Author: User

 

Web. config

1

 

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>

<! --
How to configure ASP. NET ApplicationsProgramFor more information, see
HTTP://Go.microsoft.com/fwlink /? Linkid = 169433
-->

<Configuration>
<Deleetask>
<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 " Connectionstring = " Data Source = 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>

 

 

2

 

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>

<! --
For more information about how to configure ASP. NET applications, visit
HTTP://Go.microsoft.com/fwlink /? Linkid = 169433
-->

<Configuration>
<Deleetask>
<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 = " />
<Add name = " 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>

 

 

Backend 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 = system. 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. etettings [ " Accessdbpath " ]) +" ; " ;
Public Static Readonly String Connstr3 = " Provider = Microsoft. Jet. oledb.4.0; Data Source = " + Httpcontext. Current. server. mappath ( " ~ /App_data/guestbook. MDB " );
Protected Void Page_load ( Object Sender, eventargs E)
{

}

Protected Void Button#click ( Object Sender, eventargs E)
{
Oledbconnection conn = New Oledbconnection (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 ();
}

}
}
}

 

 

Connstr1, connstr2, connstr3

Which of the three connection strings can be used.

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.