Search code examples
asp.netsql-serversqlconnection

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>

Solution

  • 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.