search
HomeBackend DevelopmentC#.Net TutorialHow to configure the database connection string connectionStrings section in ASP.NET web.config

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>

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>

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>

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>
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
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
Developing with C# .NET: A Practical Guide and ExamplesDeveloping with C# .NET: A Practical Guide and ExamplesMay 12, 2025 am 12:16 AM

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

C# .NET: Understanding the Microsoft .NET FrameworkC# .NET: Understanding the Microsoft .NET FrameworkMay 11, 2025 am 12:17 AM

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

The Longevity of C# .NET: Reasons for its Enduring PopularityThe Longevity of C# .NET: Reasons for its Enduring PopularityMay 10, 2025 am 12:12 AM

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

See all articles

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool