For accessing the database, for getting the data or inserting the data we basically use SQLDataAdapter.Fill(), ExecuteNonQuery(), and ExecuteScalar(), I have seen when I was a fresher, many developers were confused between them and don’t know which one is suitable for which condition.
Therefore, today I will explain especially for beginners that when choosing one of these and why.
Nowadays, most of the application is connected with the database. Through the application, we also modify the data, get the data. When we talk about getting the data from the database, it can be single data, a list of data, or only a single value. The data can be any of these. So, basically we use SqlCommand class to pass the query to the database.
There are three methods available in ADP.NET which are used to access your database. And these are as follows.
- SqlDataAdapter.Fill()
- ExecuteNonQuery()
- ExecuteScalar()
Create a Test database with Employee Table

- <connectionStrings>
- <add name="DefaultConnection" connectionString="Data Source=Mukesh-Pc;Initial Catalog=Test; User Id=sa; Password=******;" providerName="System.Data.SqlClient" />
- </connectionStrings>
SqlDataAdapter.Fill()
- CREATE PROCEDURE GetEmployeeList
- AS
- BEGIN
- SELECT * FROM dbo.Employees
- END

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
- namespace EmployeeDemo
- {
- public partial class Emploee: System.Web.UI.Page
- {
- string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- GetEmployeesList();
- }
- }
- public DataSet GetEmployeesList()
- {
- DataSet dsEmployee = new DataSet();
- using(SqlConnection con = new SqlConnection(connectionString))
- {
- SqlCommand objSqlCommand = new SqlCommand("GetEmployeeList", con);
- objSqlCommand.CommandType = CommandType.StoredProcedure;
- SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);
- try
- {
- objSqlDataAdapter.Fill(dsEmployee);
- dsEmployee.Tables[0].TableName = "Employees";
- grvEmployee.DataSource = dsEmployee;
- grvEmployee.DataBind();
- }
- catch (Exception ex)
- {
- return dsEmployee;
- }
- }
- return dsEmployee;
- }
- }
- }
ExecuteNonQuery()
- public int AddEmployee(string name, string emailId, string age, string address, int DepartmentId)
- {
- int i = 0;
- using(SqlConnection con = new SqlConnection(connectionString))
- {
- con.Open();
- SqlCommand objSqlCommand = new SqlCommand("Insert into Employees values ('" + name + "','" + emailId + "','" + age + "','" + address + "','" + DepartmentId + "')", con);
- try
- {
- i = objSqlCommand.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- con.Close();
- }
- }
- return i;
- }

ExecuteScalar()
- public string GetEmployeeName(int id)
- {
- string employeeName = string.Empty;
- using(SqlConnection con = new SqlConnection(connectionString))
- {
- con.Open();
- SqlCommand objSqlCommand = new SqlCommand("select name from employees where id='" + id + "'", con);
- try
- {
- employeeName = Convert.ToString(objSqlCommand.ExecuteScalar());
- }
- catch (Exception ex)
- {
- con.Close();
- }
- }
- return employeeName;
- }


Arul RPosted Jan 17, 2016, 3:15 AM
Nice share
Aishwarya DPosted Dec 7, 2015, 12:41 AM
Nice share
Santhakumar MunuswamyPosted Dec 4, 2015, 7:22 AM
Good one