Copy Code code as follows:
<configuration>
<appSettings>
<add key= "connstr1" value= "Data source=.;i Nitial catalog=dbname;integrated security=true "/>
<add key= "connstr2" value= "...". >
</appSettings>
<connectionStrings>
<add name= "CONNSTR3" connectionstring= "..."/>
<add name= "CONNSTR4" connectionstring= "..." Providername= "System.Data.Sqlclient"
</connectionStrings>
</configuration>
As shown in the code above: two ways for appsettings and connectionstrings
AppSettings:
① It was asp.net1.1, used in vs2003.
② in the form of key-value pairs, key and value. Not only can you save the connection string, but you can also store some configuration items.
③ in appsettings, you cannot use Providername= "System.Data ..." (but if you want to use it, just write it in value and pass the past as a value).
④ in the background value method code:
String conn=system.configuration.configurationmanager.appsettings["ConnStr";]
ConnectionStrings:
① It is new in asp.net2.0.
② is also similar to the form of key-value pairs, using the name and ConnectionString, which generally store the connection string.
③ in connectionstrings, you can use ProviderName.
④ in the background code, the way to take value:
String conn=system.configuration.configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
Since connectionstrings is a 2.0 version, it's certainly better than appsettings:
Said online:
① can encrypt the connection string, using one of MS's encryption tools.
② a data source control that can be directly determined, without having to write code to read it and assign it to the control.
③ can easily replace the database platform, such as the Oracle database, just modify the ProviderName
ProviderName what is its role?
Let's take a look at the parameter values of the ProviderName first.
①providername= "System.Data.SqlClient"----instructions are using the MSSQLSERVER database
②providername= "System.Data.SqlLite"----instructions are using the Sqllite database
③providername= "System.Data.OracleClient"----instructions are using an Oracle database
or providername= "System.Data.Oracle.DataAccess.Client"----ditto
④providername= "System.Data.OleDb"----instructions are using an Access database
ProviderName can write or write.
When do we use providername?
For example, we are now going to do a project that will sell for two businesses in the future: A and B. This is an uncertainty, and a uses ORACLE,B to use SQL Server. So
① database: We need to build two libraries, one with Oracle and one with SQL Server.
② Program: We generally do not write two systems for them to use, we will certainly judge, first determine what they are using the database, and then in the program to execute what kind of database script.
③web.config Code:
Copy Code code as follows:
<configuration>
<connectionStrings>
<add name= "ConnStr" connectionstring= "Data source=.;i Nitial catalog=mydb;integrated security=true "providername=" System.Data.SqlClient "/>"
</connectionStrings>
</configuration>
④ Program code: Make a judgment, if Providername= "System.Data.SqlClient" Execute SQL Server script, if providername= "System.Data.OracleClient" The Oracle database script is invoked.
Copy Code code as follows:
public static readonly String connstr = system.configuration.configurationmanager.connectionstrings["ConnStr". ProviderName;
public static string databasetype = system.configuration.configurationmanager.connectionstrings["ConnStr"]. ProviderName;
public static int ExecuteNonQuery (CommandType commandtype, string commandtext, params System.Data.OleDb.OleDbParameter [] parm)
{
int num = 0;
if (DatabaseType = = "System.Data.SqlClient")
{
To execute a database script for Microsoft SQL Server here
}
else if (DatabaseType = "System.Data.OracleClient")
{
Oracle database scripts are executed here
}
return num;
}