Sql helper class in asp.net

Vishal Nagra picture Vishal Nagra · Jul 24, 2013 · Viewed 16.3k times · Source

Why should we use sql helper class in our application.What difference between sql helper class and simple class.In which situation sql Helper should used.Please define structure of class.

Answer

Karl Anderson picture Karl Anderson · Jul 24, 2013

SqlHelper is intended to consolidate mundane, repetitive code that is written time and time again in data access layer components of ADO.NET applications, like this:

using Microsoft.ApplicationBlocks.Data;
SqlHelper.ExecuteNonQuery(connection,"INSERT_PERSON", 
    new SqlParameter("@Name",txtName.Text),
    new SqlParameter("@Age",txtAge.Text));

Instead of this:

string connectionString = (string) 
ConfigurationSettings.AppSettings["ConnectionString"]; 
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("INSERT_PERSON",connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Name",SqlDbType.NVarChar,50));
command.Parameters["@Name"].Value = txtName.Text;
command.Parameters.Add(new SqlParameter("@Age",SqlDbType.NVarChar,10));
command.Parameters["@Age"].Value = txtAge.Text;
connection.Open();
command.ExecuteNonQuery();
connection.Close();

It is part of the Microsoft Application Blocks framework.