What is SqlHelper?
SqlHelper is a utility class which contains public methods ExecuteNonQuery, ExecuteDataSet, ExecuteDataTable and ExecuteScalar. Using those methods we can perform database operations like insert, delete, update and select.
What is the advantage of using SqlHelper?
By using SqlHelper we can perform a database operation by writing minimum lines of code.
How to set connection string?
You can follow either of the below two steps:
1. You can simply add app setting with key “conString” in web.config
<configuration>
<appSettings>
<add key="conString" value="Data Source=<servername>;Initial Catalog=<databasename>;User Id=<userid>;Password=<password>"/>
</appSettings>
</configuration>
Internally SqlHelper will fetch the connection string from the app settings as the below line of code is written inside it.
private string connectionString = ConfigurationManager.AppSettings["conString"];
2. Or by setting the property ConnectionString.
SqlHelper sqlHelper = new SqlHelper();
sqlHelper.ConnectionString = "Data Source=<servername>;Initial Catalog=<databasename>;User Id=<userid>;Password=<password>";
How to open and close connection?
SqlHelper sqlHelper = new SqlHelper();
sqlHelper.OpenConnection();
// Perform database operations.
sqlHelper.CloseConnection();
About ExecuteNonQuery method
ExecuteNonQuery is used to perform database operations like insert or update or delete. There are two overloaded methods for ExecuteNonQuery, they are:
1. public int ExecuteNonQuery(string commandText, CommandType cmdType)
2. public int ExecuteNonQuery(string commandText, CommandType cmdType, params SqlParameter[] commandParameters)
The second overloaded method accepts array of parameters to attach to SqlCommand object.
ExecuteNonQuery returns number of rows affected.
Parameter “commandText” can be a text query ex: "DELETE FROM DemoTable WHERE Id = @Id" or name of the stored procedure ex: "spDelDemoTable".
Parameter “cmdType” is to specify whether command text is text query or stored procedure name i.e. CommandType.Text or CommandType.StoredProcedure.
Parameter “commandParameters” are array of parameters to attach to SqlCommand object.
Example:
// inline query
string query = "DELETE FROM DemoTable WHERE Id = @Id";
// stored procedure name
// string spName = "spDelDemoTable";
// create an instance of SqlParameter with paramerter name and its value
SqlParameter parId = new SqlParameter("@Id", "19");
// create an instance of SqlHelper
SqlHelper sqlHelper = new SqlHelper();
// open the connection
sqlHelper.OpenConnection();
// call ExecuteNonQuery and specify parameters command text, command type and sql parameter
int result = sqlHelper.ExecuteNonQuery(query, CommandType.Text, parId);
// int result = sqlHelper.ExecuteNonQuery(spName, CommandType.StoredProcedure, parId);
// close the connection
sqlHelper.CloseConnection();
In the above example, a row is deleted from DemoTable with specific Id. We can use either inline query or stored procedure name as command text and specify the command type accordingly.
ExecuteNonQuery returns an integer i.e. number of rows affected.
About ExecuteDataSet method
ExecuteDataSet is used to retrieve data from database, it returns a DataSet. There are two overloaded methods for ExecuteDataSet, they are:
1. public DataSet ExecuteDataSet (string commandText, CommandType cmdType)
2. public DataSet ExecuteDataSet (string commandText, CommandType cmdType, params SqlParameter[] commandParameters)
The second overloaded method accepts array of parameters to attach to SqlCommand object.
Parameter “commandText” can be a text query ex: "SELECT * FROM DemoTable" or name of the stored procedure ex: "spGetDemoTable".
Parameter “cmdType” is to specify whether command text is text query or stored procedure name i.e. CommandType.Text or CommandType.StoredProcedure.
Parameter “commandParameters” are array of parameters to attach to SqlCommand object.
Example:
// inline query
string query = "SELECT * FROM DemoTable";
// stored procedure name
// string spName = "spGetDemoTable";
// create an instance of SqlHelper
SqlHelper sqlHelper = new SqlHelper();
// open the connection
sqlHelper.OpenConnection();
// call ExecuteDataSet and specify parameters command text, command type
DataSet dsResult = sqlHelper.ExecuteDataSet (query, CommandType.Text);
// DataSet dsResult = sqlHelper.ExecuteDataSet (spName, CommandType.StoredProcedure);
// close the connection
sqlHelper.CloseConnection();
In the above example, all the rows are retrieved from DemoTable. We can use either inline query or stored procedure name as command text and specify the command type accordingly.
ExecuteDataSet returns a DataSet which can be used to set as datasource for repeater, gridview, etc.
About ExecuteDataTable method
Same as ExecuteDataSet method, ExecuteDataSet returns a DataSet but ExecuteDataTable returns a DataTable.
About ExecuteScalar method
ExecuteScalar is used to retrieve a single value from database, it returns an object. There are two overloaded methods for ExecuteScalar, they are:
1. public object ExecuteScalar (string commandText, CommandType cmdType)
2. public object ExecuteScalar (string commandText, CommandType cmdType, params SqlParameter[] commandParameters)
The second overloaded method accepts array of parameters to attach to SqlCommand object.
Parameter “commandText” can be a text query ex: "SELECT COUNT(*) FROM DemoTable" or name of the stored procedure ex: "spGetRecCount".
Parameter “cmdType” is to specify whether command text is text query or stored procedure name i.e. CommandType.Text or CommandType.StoredProcedure.
Parameter “commandParameters” are array of parameters to attach to SqlCommand object.
Example:
// inline query
string query = " SELECT COUNT(*) FROM DemoTable";
// stored procedure name
// string spName = "spGetRecCount";
// create an instance of SqlHelper
SqlHelper sqlHelper = new SqlHelper();
// open the connection
sqlHelper.OpenConnection();
// call ExecuteScalar and specify parameters command text, command type
object result = sqlHelper.ExecuteScalar (query, CommandType.Text);
// object result = sqlHelper.ExecuteScalar (spName, CommandType.StoredProcedure);
// close the connection
sqlHelper.CloseConnection();
In the above example, number of rows in DemoTable is retrieved. We can use either inline query or stored procedure name as command text and specify the command type accordingly.
ExecuteScalar returns an object which can be converted to int or string or to any other datatype and used accordingly.