What is App.config File in .NET and state with the example?

Asked 05-Jul-2021
Viewed 486 times

0

 What is application.config file and state with a proper example?


1 Answer


1

App.config file
 It is also a special type of configuration which is use in window service, window application, console application and WPF application. This configuration set all setting of this type of application in .net programming. It parse at compile time means that if you edit the configuration file at running time then you need to restart the application for reload the settings.
In this configuration store many types of setting.
• Database setting
• Connection string
• Error handling
• Managing the log files
• Security
• External file configuration
Example
<?xml version='1.0' encoding='utf-8' ?>
<configuration>
    <startup>
        <supportedRuntime version='v4.0' sku='.NETFramework,Version=v4.5' />
    </startup>
</configuration>
Or
<?xml version='1.0'?>
<configuration>
 <connectionStrings>
  <add name='MyKey'
 connectionString='Data Source=localhost;Initial Catalog=ABC;'
 providerName='System.Data.SqlClient'/>
     </connectionStrings>
</configuration>
Machine configuration
 It is a special type of configuration file this file is create in operating system. This store the machine configuration. Like device name, version, and only one machine file build in the system and store high level of system configuration.
Connection string
 The connection string use in .net programming for connecting to .net application with database. This connection string passes in web.config and app.config file using the connectionstring tag in config file. This connection string tag take ADD tag for adding the attribute in connection string. We can add more than on connection string, the application use this string at runtime for fetching database value for database.
In ADD tag have attributes for adding the connection details
• Name
• Connection string
• Provider name
In the connection string attribute we can give
• Server
• Database name
• Initial catalog
• Connection User id
• Connection Password
• Connection Time out
• Integrated security
• Connection pool size
• Encryption
<?xml version='1.0' encoding='utf-8'?>
<configuration>
  <connectionStrings>
     <add name='myConnection' connectionString='server=localhost;database=mydatabase;' />
  </connectionStrings>
</configuration>
Fetching the connection string is done using the ConfigurationManager in class program.
string conn = ConfigurationManager.ConnectionStrings['myConnection'].ConnectionString;
Example of database connection
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn ;
   connetionString = 'Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password'
            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();
                MessageBox.Show ('Connection Open ! ');
                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show('Can not open connection ! ');
            }
        }
    }
}
Server Connection
<connectionStrings>
  <add
    name='sqlServer'
    providerName='System.Data.SqlClient'
    connectionString='Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True;' />
</connectionStrings>
Connection timeout
<connectionStrings>
  <add
    name='sqlServer'
    providerName='System.Data.SqlClient'
    connectionString='Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True;Connection Timeout=60' />
</connectionStrings>
Username and password
<connectionStrings>
  <add
    name='sqlServer'
    providerName='System.Data.SqlClient'
    connectionString='Data Source=localhost;Initial Catalog=MyDatabase;User Id=user;Password=pwd;' />
</connectionStrings>
Mysql
<connectionStrings>
  <add
    name='mySql'
    providerName='MySql.Data.MySqlClient'
    connectionString='Server=localhost;Database=MyDatabase;Uid=user;Pwd=pwd;' />
</connectionStrings>
Integrity security
<connectionStrings>
  <add
    name='mySql'
    providerName='MySql.Data.MySqlClient'
    connectionString='Server=localhost;Database=MyDatabase;IntegratedSecurity=yes;Uid=auth_windows;' />
</connectionStrings>
Connection pool
<connectionStrings>
  <add
    name='mySql'
    providerName='MySql.Data.MySqlClient'
    connectionString='...;MinimumPoolSize=10;maximumpoolsize=50;' />
</connectionStrings>