What is Connection String in Database Connection?

Asked 05-Jul-2021
Viewed 611 times

1 Answer


2

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>