Storing Connection Strings in Configuration File

Config File in C# Tutorial

In ADO.NET the connection string determines which server and which database you are connect, so if you strong code this into your application, the database cannot be renamed neither can you move the database to another computer. You can store the connection string in a separate file.






  • Defining a connection string in a configuration file 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connection string name"
         connectionString="connection details"/>
  </connectionStrings>
</configuration>



  • Connection details can be like this: 

connectionString="Data Source=.;Initial Catalog=TutorialsCode;
                  User Id=tc;Password=default"/>

  • Using connection string in an application

  SqlConnection con;
  con=new SqlConnection(ConfigurationManager.ConnectionStrings["NameConn"].ConnectionString);

If you need any help feel free to leave a comment (or) contact me.

No comments:

Post a Comment