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.
- SqlConnection class : It is used to establish an open connection to the SQL Server database.
- SqlCommand class : It is allows you to query and send commands to a database.
- Open() Method : Open a database connection with the property settings specified by the connectionString.
- Close() Method : Closes the connection to the database.
- Fill() Method : The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property.
- DataTable class : It is a database table representation and provides a collection of columns and rows to store data in a grid form.
- 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.
- SqlDataReader class : Provides a way of reading a forward-only stream of rows from a SQL Server database.
- 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;
}
}
}
0 Comments
Enter Your Comment