C # connection string and Application of APP. config when connecting to the Access Database

Source: Internet
Author: User

# Region connection string

The connection string contains 2

1 absolute path
String strcon = @ "provider = Microsoft. jet. oledb.4.0; Data Source = D: \ Program book SOFTWARE \ C # program code \ Access database operation \ Addresslist. mdb "; // absolute path

2 relative path, where environment. currentdirectory is the execution path of the program exe.
String strcon = @ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + environment. currentdirectory + "\ Addresslist. mdb"; // relative path

3app. config

String strcon = configurationmanager. deleettings ["addaccess"]. tostring (); // method 3: Use the app. config file to add references and add using.

 

Bytes ------------------------------------------------------------------------------------

App

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>

<Connectionstrings>

<! -- SQL2000 connection string -->
<Add name = "consql" connectionstring = "Data Source = localhost; database = fuxiaojun; uid = sa; Pwd = ruheshi0372"
Providername = "system. Data. sqlclient"/>

</Connectionstrings>

<Deleetask>
<! -- SQL2000 connection string -->
<! -- <Add key = "testaddsql2000"
Value = "Server = localhost; database = fuxiaojun; user id = sa; Password = ruheshi0372"/> -->

<! -- Access connection string -->
<Add key = "addaccess"
Value = "provider = Microsoft. Jet. oledb.4.0; Data Source = D: \ Program book SOFTWARE \ C # program code \ Access database operation \ Addresslist. mdb"/>

<! -- Access relative path data source = | datadirectory | \ database. mdb database is stored in the app_data folder -->
<Add name = "acccnstr" connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = | datadirectory | \ date. MDB; persist Security info = true "providername =" system. data. oledb "/>


</Appsettings>

</Configuration>

Call

C # code
String connstr = configurationmanager. connectionstrings ["connstr"]. tostring ();
Oledbconnection conn = new oledbconnection (connstr );

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.