Implement Search Functionality Using Connected & Disconnected Architecture In ADO.NET - C#.

In this article we will how to create search functionality using connected & disconnected architecture in ADO.NET in C#.

We will using some classes and method for implementing search functionality using connected and disconnected architecture.

  1.     SqlConnection class : It is used to establish an open connection to the SQL Server database.
  2.     SqlCommand class : It is allows you to query and send commands to a database.
  3.     Open() Method : Open a database connection with the property settings specified by the connectionString.
  4.     Close() Method : Closes the connection to the database.
  5.     Fill() Method : The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property.
  6.     DataTable class : It is a database table representation and provides a collection of columns and rows to store data in a grid form.
  7.     SqlDataAdapter class : Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.
  8.     SqlDataReader class : Provides a way of reading a forward-only stream of rows from a SQL Server database.
  9.     Add() Method : Add DataRow in the DataTable.

Example :

using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace getsolutionhubDemo
{
    public partial class searchFuncnality : Form
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mayank\source\repos\getsolutionhubDemo\getsolutionhubDemo\searchDB.mdf;Integrated Security=True");
        public searchFuncnality()
        {
            InitializeComponent();
        }

        private void connectedSearchBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            connectedGridView.Rows.Clear();
            string str = "select * from searchTable where name like '%"+connectedSearchBox.Text+"%'";
            conn.Open(); 
            SqlCommand cmd = new SqlCommand(str, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                connectedGridView.Rows.Add(dr[0],dr[1]);
            }
            conn.Close();
        }

        private void disconnectedSearchBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = "select * from searchTable where name like '%" + disconnectedSearchBox.Text + "%'";
            DataTable dt = new DataTable();
            SqlDataAdapter dr = new SqlDataAdapter(str, conn);
            dr.Fill(dt);
            disconnectedGridView.DataSource = dt;
        }

        private void searchFuncnality_Load(object sender, System.EventArgs e)
        {

            string str = "select * from searchTable";
            SqlCommand cmd = new SqlCommand(str, conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                connectedGridView.Rows.Add(sdr[0], sdr[1]);
            }
            conn.Close();

            SqlDataAdapter adapter = new SqlDataAdapter(str, conn);
            DataTable dt2 = new DataTable();
            adapter.Fill(dt2);
            disconnectedGridView.DataSource = dt2;
        }
    }
}

Output:

Search Funcationality using connected & disconeected architecture in ADO.NET - c#