ADO.Net SQLCommand - ExecuteReader - DotNet and DataBase

Wednesday, 1 August 2018

ADO.Net SQLCommand - ExecuteReader




The ExecuteReader() in .Netframework is a SqlCommand Object transfers the SQL queries to the Connection Object and generates a SqlDataReader Object based on the SQL statement supplied. When the ExecuteReader method in SqlCommand Object invokes, it will instantiate the SqlClient.SqlDataReader Object.

Example
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace EmployeeTimeSheet
{
    public partial class InsertEmployees : Form
    {
        public InsertEmployees()
        {
            InitializeComponent();
        }       

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SqlDataReader reader;
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT ID,Name FROM EmployeeInfo", con);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                MessageBox.Show(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.GetValue(2));
            }
            reader.Close();
            con.Close();
        }
    }
}

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.