Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make different settings for ASP.NET1.1 and ASP.NET2.0 database connection strings

Shulou Source: shulou.com Published: 2022-06-02 22:08:16 10月23日 Update

How to set different connection strings between ASP.NET1.1 and ASP.NET2.0 database? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

In the asp.net1.1 era, database connection strings were written under the appSettings section of the web.config file. The specific words are as follows:

< configuration>

< appSettings>

< add key="ConnString" value="Server=(local);Database=pubs;User Id=sa;Password=sa;"/>

< /appSettings>

Of course, in the appsetings section, you can store not only database connection strings, but also some custom configurations related to the program.

information。 When in use, you should first using System.Configuration; and then string strConn=Configuration.AppSettings ["ConnString"].

From the ASP.NET1.1 era to the asp.net2.0 era, not only the appSettings segment is retained, but also a new connectionStrings segment is added. Generally, the database connection string used in the program is placed under this section, and other custom configuration information is placed under the appSettings section. At the same time, Microsoft does not recommend using the Configuration class, but the ConfigurationManager class. To use it, add the following under the connectionStrings section:

< add name="ConnString" connectionString="Server=(local);Database=pubs;User Id=sa;Password=sa;" providerName="System.Data.SqlClient"/>

In use, using System.Configuration; is still used first, but it must be noted that references must be added in the project, System.Configuration must be found in the pop-up dialog box, and click to add, otherwise, the ConfigurationManager class cannot be used.

Finished this part, is how to get the database connection string, here we generally use the following methods to get the connection string.

String strConn=ConfigurationManager.ConnectionStrings ["ConnString"] .ToString ()

Of course, indexes starting from 0 can also be used in square brackets, but it should be noted that the configuration in web.config in asp.net2.0 is inherited from machine.config in the machine, and there is a database connection string pointing to SQL2005 Express by default in the machine.config file, so when you directly use ConfigurationManager.ConnectionStrings [0] .ToString () Instead of the database connection string you wrote in web.config, you get the database connection string that points to SQL2005 Express. I got stuck in this place for a while, and then I used breakpoint debugging to find this problem, so it's better to use the name of the connection string directly, and there will be no problem.

This is the answer to the question about how to set up ASP.NET1.1 and ASP.NET2.0 database connection strings. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Tags: Characters strings data databases problems sections methods times or configurations different information content points files more programs but store help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia MariaDB macOS Shulou Tech Info