简体   繁体   中英

What is the difference between defining SqlConnection in code behind file and in web.config file?

What is the difference between SqlConnection in code behind and connection string in web.config ?

Code-behind:

Dim con As SqlConnection = New SqlConnection("Initial Catalog=Election;Data Source=xxx;User ID=xxx;Password=xxx;Persist Security Info=False; Connect Timeout=60")

web.config :

<connectionStrings>
    <add name="conn" 
         connectionString="Initial Catalog=Election;Data Source=xxx;User ID=xxx;Password=xxx;Persist Security Info=False; Connect Timeout=60;Pooling=False" />
</connectionStrings>

The SqlConnection in your code is what you need to connect to the database at runtime. However, initializing a SqlConnection in this way will mean that your application can only ever connect to the SQL database in this way. If the password changes, or if the application requires a different connection, the application will have to be rewritten and recompiled.

The connection string in the configuration file is a mechanism for being able to change the SQL connection string without having to recompile the application.

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conn"))

The connection string is a string that contains information on how to connect to a database. The SqlConnection object is an object that actually contains the database connection and is used to execute statements against it.

As I know,

What is the difference between SqlConnection in code behind and connection string in the web config?

There is No difference in the current behavior or working, you can put the connection string in the code itself or in the web.config file!

Connection string in Code itself:

If you put the connection string in the code itself like:

Dim con As SqlConnection = New SqlConnection("Initial Catalog=Election;Data Source=xxx;User ID=xxx;Password=xxx;Persist Security Info=False; Connect Timeout=60")

In case of a password change or sometimes you want to change the Connection Timeout = 90 in that case you have to build an application and have to publish to get the updated changes.

Connection string in web.config file:

<connectionStrings>
    <add name="constr" 
         connectionString="Initial Catalog=Election;Data Source=xxx;User ID=xxx;Password=xxx;Persist Security Info=False; Connect Timeout=60;Pooling=False" />
</connectionStrings>

accessing in the code behind:

string ConStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
Dim con As SqlConnection = New SqlConnection(ConStr)

In case of the password change or any modification in the connection string, just change in web.config file because we are reading the connection string dynamically from web.config so every time you will get the string that is in web.config

There are not the major differences between both of them. Both are the two different mechanism of how to provide connection string to the object SQLCommand.

It is more feasible to use and change the in web.config to the production server without any downtime as per the requirement.

Several times as we know we need to change server name, database name, password, timeout etc. When it is defined in the code behind then you need to first recompile and publish it to the server which is a time and resource consuming.

We use connection string in code behind usually when we need to provide connection dynamically for example selecting from drop down or want to change based on the date, etc. Although this can also be done in web.config with defining different connection string and deciding which we need to use based on the condition in code behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM