web. config configures the method of the connection string

  • 2020-10-23 20:04:34
  • OfStack


<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="System.Data.Sqlclient"
   </connectionStrings>
</configuration>

As shown in the code above: the two ways are appSettings and connectionStrings

appSettings:

It was used in asp. net1.1. It was used in vs2003

The equivalent of a key-value pair, key and value, is stored in. Not only can you store connection strings, you can also store 1 configuration item.

In appSettings, ProviderName="System.Data..." (But if you want to use it, you can just write it in value and pass it as a value.)

Value in the background with code:

string conn=System.Configuration.ConfigurationManager.AppSettings["connstr";]

connectionStrings:

It is a new addition to ES36en.net2.0.

It is similar to the key-value pair, using name and connectionString, and 1 generally stores connection strings.

In connectionStrings can, can use providerName.

In the background code, the way to value:

string conn=System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

Since connectionStrings is version 2.0, it's definitely better than appsettings:

What the Internet says:

The connection string can be encrypted, using MS's 1 encryption tool.
(2) can directly bond the data source control, and do not have to write code to read out and then assign values to the control.
It is convenient to change the database platform, such as Oracle database, only need to modify providerName

What does providerName do?

Let's look at the parameter value of providerName first.

providerName="System.Data.SqlClient" -- indicating the use of MSSQLServer database
providerName="System.Data.SqlLite" -- indicates that SQLLite database is used
providerName=" System. Data. OracleClient" -- indicates that Oracle database is used
Or providerName = "System Data. Oracle. DataAccess. Client" - same as above
providerName=" System. Data. OleDb" -- indicating the use of Access database

Write on or off.

When do we use providerName?

For example, we are going to do one project now, and we will sell two enterprises to use A and B in the future. So there are uncertainties, A USES Oracle, B USES SQLserver. so

Database: we need to build two libraries, one with oracle, one with Sqlserver.

2 procedures: we generally do not write two systems to let them use, we will certainly go to judge, first judge what database they use, and then execute what kind of database script in the program.

(3) web. config code:


<configuration>
  <connectionStrings>
    <add name="connStr" connectionString="Data Source=.;Initial Catalog=mydb;Integrated Security=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Program code: to judge, if providerName=" System.Data.SqlClient "then execute SQLServer script, if providerName=" System.Data.OracleClient" then call Oracle database script.


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")
    {
// Here to perform Microsoft SQLServer Database script 
    }
    else if (databaseType == "System.Data.OracleClient")
    {
// Here to perform Oracle Database script 
    }
    return num;
}


Related articles: