Web. config database connection, web. config

Source: Internet
Author: User

Web. config database connection, web. config

Method 1: connectionsStrings

First configure the web. config file

<Deployments>

<ConnectionStrings>

<Add name = "Test" connectionString = "server =.; database = Testbase; uid = sa; pwd = asdf123 _;">

</ConectionStrings>

<Deployments>

The server indicates the database address, the local database can be represented by "." or "localhost", the database indicates the database name, the uid indicates the name of the database to log on to, and the pwd indicates the password to log on to the database;

The aspx page is simpler.

First reference the namespace using System. Web. Configuration;

String con = ConfigurationManager. ConectionStrings ["Test"]. ConnectionString;

If (! String. IsNullOrEmpty (con ))
{
TextBox1.Text = "connected to the database! ";
}
Else
{
TextBox1.Text = "failed to connect to the database! ";
}

Method 2: appSettings

Web. config Configuration

<Deployments>

<Deleetask>

<Add key = "Test" value = "server =.; database = Testbase; uid = sa; pwd = asdf123 _;/>

</AppSettings>

</Deployments>

The server indicates the database address, the local database can be represented by "." or "localhost", the database indicates the database name, the uid indicates the name of the database to log on to, and the pwd indicates the password to log on to the database;

Aspx page

First, the application namespace first references the namespace using System. Web. Configuration;

String con = configurationManager. configuretting ["Test"];

If (! String. IsNullOrEmpty (con ))

{
TextBox1.Text = "connected to the database! ";
}
Else
{
TextBox1.Text = "failed to connect to the database! ";
}

 

1. Integrated Windows Authentication syntax example

String con = "server =.; database = myschool; integrated security = SSPI ";

2. The Windows Authentication Mode in SQL Server 2005 is as follows:

String con = "server =.; database = school; uid = sa; pwd = asdf123 _";

3. the SQL Server Authentication Mode in SQL Server 2005 is as follows:

String con = "data source =.; initial catalog = school; user id = sa; pwd = asdf123 _";

 

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.