How to configure the connection between the SQL Server database and Web. config, and configure the database through webconfig

Source: Internet
Author: User
Tags sql server connection string

How to configure the connection between the SQL Server database and Web. config, and configure the database through webconfig

This article mainly introduces Web. config correctly configure the actual wiping steps for the SQL Server database connection. in Figure 5-6, select "Add new Web with debugging enabled. select the "config File" button. in Figure 5-6, click the "OK" button and you will see it in the "Solution Explorer" dialog box.

Create a "Web. config" file in the root directory of the Web application ,.

Figure 1.1 "debugging not enabled" dialog box

Figure 1.2 generate the "Web. config" File

The settings 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.

Configure SQL Server database connection

In the "Web. config" file, you can configure the SQL Server database connection string in the <connectionStrings> </connectionStrings> node, as shown in the table.

The following table shows the properties used to configure the SQL Server connection string.

GenusDescription

Data Source specifies the name of the Database Server

Database Name of the Database to be connected

Uid specifies the username of the database server to log on

Pwd specifies the password of the database server to log on

When you set the properties in Table 5-7 to connect to the database, SQL Server authentication is used. Sometimes Windows authentication is used. In this case, you must configure the attributes used by the SQL Server connection string to be Data Source (specify the database Server name) and Initial Catalog (specify the database name to be connected) and Integrated Security (whether to use Integrated Windows Authentication ).

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

Create a website named "Web configtest". The Default homepage is "Default. aspx ".

Add the following code to the <etettings> </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 the "design" View of "Default. aspx" to display the data bound to it.

Add the code to the "Default. aspx. cs" file to hide the code and bind the data in the database to the GridView control. The Code is as follows.

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 above content introduces how to configure the SQL Server database connection in Web. config. I hope you will get some benefits.

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.