<configuration>
<appSettings>
<add key="connstr1" value="Data Source=.;Initial Catalog=DBName;Integrated Security=true"/>
<add key="connstr2" value=".........">
</appSettings>
<connectionStrings>
<add name="connstr3" connectionString="........" />
<add name="connstr4" connectionString="......" providerName=""
</connectionStrings>
</configuration>
As shown in the above code: two methods are appSettings and connectionStrings
appSettings:
① It was used when asp.net1.1 and in vs2003
② The forms of key and value stored in it are equivalent to key-value pairs, key and value. Not only can you store connection strings, but you can also store some configuration items.
③In appSettings, ProviderName="..." cannot be used (but it is OK if you want to use it, just write it in the value and pass it as a value)
④ Use code to get values in the background:
string conn=["connstr";]
connectionStrings:
① It is newly added in asp.net2.0.
② The ones stored in it are also similar to the key-value pairs, using name and connectionString, and generally the connection string is stored.
③ In connectionStrings, you can use providerName.
④In the background code, the way to get the value:
string conn=["connstr"].ConnectionString;
Since connectionStrings is from version 2.0, it is definitely more beneficial than appsettings:
This is what the Internet says:
① You can encrypt the connection string and use an encryption tool from MS.
② You can directly define the data source control without having to write code and then assign it to the control.
③ It is convenient to replace the database platform. If it is replaced with an Oracle database, you only need to modify the providerName
What is the function of providerName?
Let's first look at the parameter value of providerName.
①providerName="" ---Indicates that the MSSQLServer database is used
②providerName="" ---Indicates that SQLLite database is used
③providerName="" ---Indicates that the Oracle database is used
or providerName="" ---Same as above
④providerName="" ---Indicates that the Access database is used
ProviderName can be written or not.
When should we use providerName?
For example, we are going to do a project now, and we will sell two companies to use it in the future: A and B. This is uncertain. A uses Oracle, B uses SQLserver. so
① Database: We need to build two libraries, one using oracle and the other using Sqlserver.
②Program: We generally don’t write two systems for them to use. We will definitely judge, first determine what database they are using, and then execute what kind of database scripts in the program.
③Code:
<configuration>
<connectionStrings>
<add name="connStr" connectionString="Data Source=.;Initial Catalog=mydb;Integrated Security=true" providerName=""/>
</connectionStrings>
</configuration>
④ Program code: Make judgments. If providerName="", execute the SQLServer script, and if providerName="", call Oracle's database script.
public static readonly string connStr = ["connStr"].ProviderName;
public static string databaseType = ["connStr"].ProviderName;
public static int ExecuteNonQuery(CommandType commandType, string commandText, params [] parm)
{
int num = 0;
if (databaseType == "")
{
//Execute the database script of Microsoft SQLServer here
}
else if (databaseType == "")
{
//Execute Oracle's database script here
}
return num;
}