SQL Server database connection Web.config How to configure _ Practical tips

Source: Internet
Author: User
Tags sql server connection string connectionstrings

This article focuses on the actual wipe steps for web.config correctly configuring the SQL Server database connection, in Figure 5-6, select the Add new debug-enabled Web.config file radio button, and in Figure 5-6, after clicking OK, in the Solution Explorer dialog box to see the.

A "Web.config" file was created in the root directory of the Web application, as shown in the figure.

Figure 1.1 "Debugging Not Enabled" dialog box

Figure 1.2 Generating the "Web.config" file

The settings provided in the "Web.config" file can be applied to the entire application, including subdirectories of the application. In the configuration file "Web.config", all configuration information is located between the <configuration> and </configuration>xml root nodes.

Configuring SQL Server database connections

You can configure the SQL Server database connection string in the <connectionStrings></connectionStrings> node in the Web.config file, and the properties used in the configuration process are shown in the table.

The following table is the property used to configure SQL Server connection strings

Property Description

Data Source Specifies the database server name

database specifies the names of the databases to connect to

UID Specifies the user name of the database server to log on to

PWD Specifies the password for the database server to log on to

When you set the properties in table 5-7 to connect to the database, SQL Server authentication is used. Windows authentication is sometimes used, at which point you need to configure the SQL Server connection string to use a property of data Source (specifying the database server name), Initial Catalog (specifying the database name to connect to), integrated Security (Specifies whether Integrated Windows authentication is used).

The following example shows how to configure a SQL Server database connection in the Web.config file and read the configuration information in the application. The procedure for creating the example is as follows.

Create a new Web site named "Web configtest" with the default home page named "Default.aspx."

Add the following code to the <appSettings></appSettings> node of the "Web.config" file to connect to the SQL Server database:

<appSettings> 
<add key= "sqlconn" value= "Data source=localhost;initial catalog= pubs; 
Integrated security=true "/> 
</appSettings> 

Add a GridView control in Design view of page "default.aspx" to display the data that is bound to it.

Add code in the code-behind file "Default.aspx.cs" to bind the data in the database to the GridView control, as shown in the following code.

Using System; 
Using System.Data.SqlClient; 
..... public partial class _default:system.web.ui.page 
{ 
protected void Page_Load (object sender, EventArgs e) 
{ 
String conn = configurationmanager.appsettings["Sqlconn"]; 
SqlDataAdapter SDA = new SqlDataAdapter ("select * From Publishers", 
conn); 
DataSet ds = new DataSet (); 
Sda. Fill (ds, "Publishers"); 
Gridview1.datasource = ds. tables["Publishers"]; 
Gridview1.databind (); 
} 
 

The related content above is the introduction of Web.config configuration SQL Server database connection, I hope you can get something.

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.