Home Backend Development C#.Net Tutorial How to configure the database connection string connectionStrings section in ASP.NET web.config

How to configure the database connection string connectionStrings section in ASP.NET web.config

Jan 13, 2017 pm 01:21 PM

In the root directory of the website developed by ASP.NET, there is a file named web.config. As the name suggests, this is a file that configures the entire website, and its format is XML format.
Here we mainly talk about the section in the file. The section configures the strings connected to the database. Since MS SQL Server and ASP.NET are both Microsoft products, the preferred database when developing using ASP.NET is MS SQL Server. This article only discusses the MS SQL Server connection string situation.
In the first case, when developing locally, use a local database, such as the following code

<connectionStrings>
    <add name="myConn"
connectionString ="Data Source=(LocalDB) \v11.0;AttachDbFilename=|DataDirectory| \Movies.mdf;Integrated Security=True" providerName ="System.Data.SqlClient" />
</connectionStrings>
Copy after login

Discussion:
This is the most common way to use the database connection string for local development using a PC. Among them, the
name attribute refers to the connection string name. When a database needs to be used on the website, this connection string name needs to be quoted; in this example, it is myConn; the
Data Source attribute is the database server, (LocalDB) \V11.0 indicates that the local database server is used, the version number is 11, that is, SQL Server 2012;
AttachDbFilename attribute specifies the specific data as the name and location, |DataDirectory| corresponds to the system directory App_Data in the ASP.NET website, this attribute The value description connects to the database named Movies.mdf in this directory. The mdf file name indicates that the database requires the service of the SQL Server server, but it is an independent database file itself and can be copied and pasted without the need for SQL Server. The management system (such as SSMS) performs database separation work;
Integrated Security=True" indicates integrated authentication, which is the Windows authentication method. As long as this attribute and attribute value are present, the user name and password are not required in the connection string;
providerName="System.Data.SqlClient" is the data provider
This situation is the most common situation for local development: you can directly use ASP.NET to create a database (the extension is .mdf), or you can use The ASP.NET website configuration generates the ASPNETDB.MDF database. When the database is created, create a connection string in the ASP.NET interface. You only need to provide the name of the connection string. Other attributes and attribute values ​​​​of the connection string can be automatically added to the web .config file.

In the second case, when developing locally, use the following connection string:

<connectionStrings>
    <add name="myConn" connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Copy after login

Note: The connectionString attribute here is simpler than the first case. Pay attention here. The database extension is .sdf, which is the MS SQL Compact version of the database. It can be used without opening the SQL Server service. It is compact and easy to use, but its support is not as good as .mdf (this is the MS SQL Server standard version file format ). Therefore, it is recommended to use .mdf files when developing on a normal PC. If there is no MS SQL Server service running on the machine, .sdf is a good choice.

The third situation. When developing locally, use the following code:

<connectionStrings
<add name="DefaultConnection"
connectionString ="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-
2012213181139;Integrated Security=true" providerName ="System.Data.SqlClient" />
</connectionStrings>
Copy after login

Note: The database name here has no extension, indicating that it is a database directly managed by MS SQL Server, not an independent database file (ie: if you want to copy the database, This database needs to be detached in the SQL Server management tool). This kind of connection often uses SQL Server to create the database, and then uses ASP.NET to create the connection. It is not very common when developing locally.

In the fourth case, when deploying remotely, you can see the following code:

<connectionStrings>
    <add name="myConn" connectionString="Data Source=服务器名;Initial Catalog=数据库名;uid=用户Id;pwd=用户密码;"/>
</connectionStrings>
Copy after login
Description: When you need to deploy the locally developed ASP.NET website to a remote space (such as renting space, such as your own server), this connection string is used when actually providing website access services. If you rent space, the space provider will provide the server name, database name, user name and password. Note that there is no Integrated Security=true" attribute name and attribute value, so a username and password are required.

Therefore, when using the connection string for local development (often the first method in this article) is completed After the test is successful, the connection string needs to be modified according to the data of the remote server and then uploaded to the remote server to provide real Internet access.

More database connection strings in ASP.NET web.config. For related articles on the configuration method of the connectionStrings section, please pay attention to the PHP Chinese website

!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
The Continued Relevance of C# .NET: A Look at Current Usage The Continued Relevance of C# .NET: A Look at Current Usage Apr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NET From Web to Desktop: The Versatility of C# .NET Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# as a Versatile .NET Language: Applications and Examples C# as a Versatile .NET Language: Applications and Examples Apr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

Is C# .NET Right for You? Evaluating its Applicability Is C# .NET Right for You? Evaluating its Applicability Apr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# .NET and the Future: Adapting to New Technologies C# .NET and the Future: Adapting to New Technologies Apr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C# Code within .NET: Exploring the Programming Process C# Code within .NET: Exploring the Programming Process Apr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Apr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# and the .NET Runtime: How They Work Together C# and the .NET Runtime: How They Work Together Apr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

See all articles