Home > Net >  About the dot net is used in the database connection string
About the dot net is used in the database connection string

Time:11-03

In application development, using the database is very common, especially in the relational database, to the flexibility of application deployment, database connection string does not usually hardcoded into the program, but by the application configuration file, the.net framework application configuration file provides & lt; ConnectionStrings> Node to configure the database connection string,
But I meet two more confused at work:
A, find someone with & lt; AppSettings> Configure the database connection string, when you see this, can only sigh my knowledge is too short!
Second, it is in the c # code, often see instantiation database connection object, use the following way almost all search on the net also is this way,
String connstr=ConfigurationManager. ConnectionStrings [r]. "myconn" ConnectionString;
Using (SqlConnection conn=new SqlConnection (connstr))
{
.
}

But the.net provides use the factory pattern to implement:
ConnectionStringSettings myconn=ConfigurationManager. ConnectionStrings [" myconn "];
DbProviderFactory factory=DbProviderFactories. GetFactory (myconn. ProviderName);
Using (DbConnection conn=factory. The CreateConnection ()) {
Conn. The ConnectionString=set. The ConnectionString;
.
}
Use the factory pattern is more flexible and realize multiple database support more convenient,
This is a personal opinion only,

CodePudding user response:

Good idea, is worth using for reference
  • Related