Copy codeThe Code is as follows:
<Configuration>
<Deleetask>
<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 = "System. Data. Sqlclient"
</ConnectionStrings>
</Configuration>
As shown in the code above: two methods are available: appSettings and connectionStrings.
AppSettings:
① It is used in asp. net1.1 and in vs2003
② The key-value pairs are saved in the form of keys and values. You can not only store connection strings, but also some configuration items.
③ ProviderName = "System. data ....... "(if you want to use it, you only need to write it in the value, and pass it as a value)
④ Use the code in the background value mode:
String conn = System. Configuration. ConfigurationManager. receivettings ["connstr";]
ConnectionStrings:
① It is added in asp. net2.0.
② The key-value pairs are also stored in the format of name and connectionString. Generally, the connection string is saved.
③ In connectionStrings, you can use providerName.
④ In the background code, the value method is as follows:
String conn = System. Configuration. ConfigurationManager. ConnectionStrings ["connstr"]. ConnectionString;
Since connectionStrings comes out of connectionStrings 2.0, it is certainly better than appsettings:
The Internet says:
① You can encrypt the connection string by using an encryption tool of MS.
② Data source controls that can be directly attached without having to write code to read them and then assign them to the controls.
③ You can easily change the database platform. For example, if you change to an Oracle database, you only need to modify the providerName.
What is the role of providerName?
Let's take a look at the parameter value of providerName.
① ProviderName = "System. Data. SqlClient" ---- indicates that the MSSQLServer database is used.
② ProviderName = "System. Data. SqlLite" ---- indicates that the SQLLite database is used.
③ ProviderName = "System. Data. OracleClient" ---- indicates that the Oracle database is used.
Or providerName = "System. Data. Oracle. DataAccess. Client" ---- same as above
④ ProviderName = "System. Data. OleDb" ---- indicates that the Access database is used.
ProviderName can be written or not.
When can we use providerName?
For example, if we want to create A project, we will sell two Enterprises: A and B. There are uncertainties. A uses Oracle and B uses SQLserver. So
① Database: we need to create two databases: oracle and SQL Server.
② Program: we generally do not write two systems for them to use. We will certainly judge what database they are using first, and then execute what database scripts in the program.
③ Web. config code:
Copy codeThe Code is as follows:
<Configuration>
<ConnectionStrings>
<Add name = "connStr" connectionString = "Data Source =.; Initial Catalog = mydb; Integrated Security = true" providerName = "System. Data. SqlClient"/>
</ConnectionStrings>
</Configuration>
④ Program code: make a judgment. If providerName = "System. Data. SqlClient" is used, the SQL server script is executed. If providerName = "System. Data. OracleClient" is used, the Oracle database script is called.
Copy codeThe Code is 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 ")
{
// Execute the Microsoft SQLServer database script here
}
Else if (databaseType = "System. Data. OracleClient ")
{
// Execute the Oracle database script here
}
Return num;
}