Loading, please wait...

A to Z Full Forms and Acronyms

What is Connection String in ADO.NET?

in this article we will learn about connection string. we will learn how they enable data connectivity between database and application.

The connection string is a string representation which contains information about database such as  Address or path or about where the database is located.

It helps in establishing a connection of the application with the database whose information has been passed in the connection string.

The database may be present in the same machine as of the application or even on a different machine i.e. other than the machine on which the application is being developed. In case the DATABASE is present on a different machine, we provide the IP address of the machine along with the remaining parameters within the Connection String.

If the database is present on the same machine as that of the application, then we simply pass the driver name, server name, and the database name in the connection string.

A connection string includes parameters such as the name of the driver, server name, and the name of the database. Also, it contains security information such as a username and password applied to the database.

The connection String for Microsoft SQL Server :

ConnectionString=”DataSource= ServerName;Initial Catalog= DatabaseName; UserId: username; Password= password”

Connection Layers between software application and database.

When your application connects to a database, a provider is used by the ADO.NET in establishing the connection. The connection string contains information about the database that the provider needs to know to be able to establish a connection between the database and the application.

Some points to remember:

  • A connection string consists of a series of keywords and their values and each keyword-value is separated by semicolons (;)
  • The equal sign (=) connects each keyword and its value

      Example: Keyword1=Value1;Keyword2=Value2;Keyword3=Value3;

  • The information in the connection string is passed on to the provider and then the provider is able to fetch the data from a database.

A sample code to illustrate the use of ConnectionString in the app.config file is given below:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>

<connectionStrings>
       <add name = "myDB" connectionString= "Data Source=(LocalDB);AttachDbFilename=Database1;Integrated Security=True"/>
</connectionStrings>

</configuration>

For better understanding, let us consider an example in which we have to display data in a table in our application which is present in the database.

Once, we have provided the database information to the connection string in the app.config file, let us create a separate class to use the connection string. We will use the dll System. Configuration to use the ConfigurationManager class that will help get the data from the database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace adonet1
{
    class SQLHelper
   {
        public string ConnectionString
        {
            get
            {
             return ConfigurationManager.ConnectionStrings["myDB"].ConnectionString;
            }
        }

        DataTable empTable;
        SqlDataAdapter adapter;

        public DataTable GetTable(String query)
        {
            adapter = new SqlDataAdapter(query, ConnectionString);
            empTable = new DataTable();
            adapter.Fill(empTable);
            return empTable;
        }
      }
    }

We want to get the table data to be displayed in the data grid view control as the WinForm is opened or loaded. Hence, the code for the form can be done as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace adonet1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SQLHelper sql = new SQLHelper();

        private void Form1_Load(object sender, EventArgs e)
        {
            data = sql.GetTable("select * from employee1");
            dataGridView1.DataSource = data;
        }
    }
 }

The data from the employee1 table gets displayed in our application with the help of the connection established with the help of connectionString.

A to Z Full Forms and Acronyms

Related Article