In ASP. NET 2.0, a new declarative expression syntax is used to parse a connection string value at runtime, referencing the database connection string by name. The connection string itself is stored under the <connectionstrings> Configuration section in the web. config file, so that it is easy to apply
Program All pages in.
sample program Code :
providername =" system. data. sqlclient "/>
providername =" system. data. sqlclient "/>
program code Description: In the program code of the preceding example. under the Configuration node in the config file, two database connection strings are set, pointing to the pubs and northwind sample databases respectively. Note that a data source control is introduced in 2.0, such as the sqldatasource control. We can set the connectionstring attribute of the sqldatasource control to an expression <% $ connectionstrings: pubs %>, this expression is run by ASP.. Net analyzer is parsed as a connection string. You can also specify an expression for the providername attribute of sqldatasource, for example, <% $ connectionstrings: pubs. providername %>. The specific usage and new features will be detailed in the subsequent sections. Now you have a basic understanding.
of course, we can also use the following method to directly read the database connection string from the configuration file. First, we need to reference the using system. Web. Configuration namespace, which contains classes used to set ASP. NET configurations.
string connectionstring = configurationmanager. connectionstrings ["northwind"]. connectionstring;
program code Description: In the program code of the preceding example, you can use connectionstrings ["northwind"] to read the corresponding northwind string. Similarly, you can use connectionstrings ["pubs"] to read the corresponding pubs string.