Asp. The various ways to connect a database in net

Source: Internet
Author: User
Tags access database connectionstrings

Asp. The various ways to connect a database in net

ways to connect to a SQL database:
(i), create a connection string in Web. config:
1.
<add name= "ConnectionString" connectionstring= "Data source=.\sqlexpress; attachdbfilename=| Datadirectory|\grade.mdf;integrated security=true; User instance=true "providername=" System.Data.SqlClient "/>
2.
<add name= "sampleconnectionstring" connectionString = "server=.\sqlexpress;database=sample;uid=sa;pwd=123456;" ProviderName = "System.Data.SqlClient"/>
3.
<add name = "testconnectionstring" connectionString = "Data source=.\sqlexpress;initial catalog=sample;integrated Security=true; Persist Security info=true; User Id=sa; password=123456 "providerName =" System.Data.SqlClient "/>(b), get the connection string in Aspx.cs:
1.
String Strcon = system.configuration.configurationmanager.connectionstrings["sampleconnectionstring"]. ConnectionString;
2.
String Strcon = system.configuration.configurationmanager.connectionstrings["sampleconnectionstring"]. ToString ();
3.
String Strcon = system.configuration.configurationmanager.connectionstrings["testconnectionstring"]. ToString ();(c), write the connection string directly to the page
1.
String Strcon = "Data source=.\\sqlexpress; attachdbfilename=| Datadirectory|\\grade.mdf;integrated security=true; User Instance=true ";
2.
String Strcon = "Data source=.\\sqlexpress;initial catalog=sample;integrated security=true; Persist Security info=true; User Id=sa; password=123456 ";
3.
String Strcon = "server=.\\sqlexpress;database=sample;uid=sa;pwd=123456";
ways to connect to an Access database (Yx.mdb):
(i), create a connection string in Web. config:
<add name = "accessconnectionstring" connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|\yx.mdb "providerName =" System.Data.OleDb "/>(b), get the connection string in Aspx.cs:
1.
String Strcon = system.configuration.configurationmanager.connectionstrings["accessconnectionstring"]. ToString ();
2.
String Strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|\\yx.mdb ";
3.
String Strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ("~/app_data/yx.mdb"); Note: If you are connecting to a database (DATABASE1.ACCDB) in the Access2007 version, connect using the following method:
String Strcon = "Provider=microsoft.ace.oledb.12.0;data source=" + Server.MapPath ("~/app_data/database1.accdb");

Asp. The various ways to connect a database in net

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.