Connected Mode in ADO.Net using C# with Example

Connected-mode-ado.net

Connected Mode in ADO.Net using C# with example

In Connected mode in ADO.NET architecture connection must be open using open method to access data from database and make connection alive while we perform insert, update, delete and search operation and after retrieve data from database connection must be close using close method.

In Connected Mode ADO.NET, DataReader is used to retrieve data from database and it can hold only single table data while in Disconnected ModeADO.NET, Dataset is used to hold multiple table data.So this is the limitation of Connected Mode ADO.NET. Because when you constantly trips to the database for any insert, update, delete and search operation you wish to do. This creates more traffic to the database.


Also Read:- Disconnected Mode ADO.NET using C#

Example:-
Create a Table in Database and windows form in visual studio having following fields as in screenshot:
table-connected-mode-ado.net

win-form-connected mode ado.net

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;
//use below namespace to connect with ADO.Net
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // Connected Mode ADO.Net Save button coding to Save data into Database Table.
        private void btnsave_Click(object sender, EventArgs e)
        {
            // Create object of sqlconnection class.
            SqlConnection con=new SqlConnection();
            // Set connection string property of connection object.
            con.ConnectionString = @"Data Source=MALIK\MALIK;Initial Catalog=smalik;Integrated Security=True";
            // Open connection using open method.
            con.Open();
            // Create object of sqlcommand class then pass connection string and connection object.
            SqlCommand cmd=new SqlCommand("insert into tb_test values("+Convert.ToInt32(txtrollno.Text)+",'"+txtfirstname.Text+"','"+txtlastname.Text+"')",con);
           // Execute Command using ExecuteNonQuery method for modification into table.
            cmd.ExecuteNonQuery();
            // Close connection using close method.
            con.Close();
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=MALIK\MALIK;Initial Catalog=smalik;Integrated Security=True";
            SqlCommand cmd=new SqlCommand("Select * from tb_test where Rollno="+Convert.ToInt32(txtrollno.Text)+"",con);
            con.Open();
            SqlDataReader dr =cmd.ExecuteReader();
           
            if (dr.Read())
            {
                txtfirstname.Text = dr["Firstname"].ToString();
                txtlastname.Text = dr["Lastname"].ToString();
            }
            else
            {
                MessageBox.Show("Record not Found");
            }
            con.Close();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=MALIK\MALIK;Initial Catalog=smalik;Integrated Security=True";
           
            SqlCommand cmd = new SqlCommand("Delete from tb_test where Rollno=" + Convert.ToInt32(txtrollno.Text) + "", con);
            con.Open();
            int temp=cmd.ExecuteNonQuery();
            if (temp > 0)
            {
                MessageBox.Show("Record Deleted");
            }
            else
                MessageBox.Show("No Record found");
          
            con.Close();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=MALIK\MALIK;Initial Catalog=smalik;Integrated Security=True";
            SqlCommand cmd = new SqlCommand("Update tb_test set Firstname='" + txtfirstname.Text + "',Lastname='" + txtlastname.Text + "'", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}



If you like this post please share this to your Friends.

Labels: , ,