Z Hussain

Z Hussain

  • 1.4k
  • 348
  • 16.1k

Base Class to handle basic ADO Steps

Feb 27 2019 3:35 PM
How to creat a base class or utility that handles the basics of creating a connection, command, parameters, etc. so that I don’t have that code in each method or class reduces chances for errors and simplifies updates?
 
Example: I have many Methods and I'm sure there'z a way to create a base class by atleast creating a Constructor or creating and inheriting base class
 
  1. //First Method      
  2. public void InsertToDoList(ToDoListProp ToDoListProp)  
  3.         {  
  4.             SqlConnection objConn = new SqlConnection();  
  5.             SqlCommand objCmd = new SqlCommand();  
  6.             SqlDataAdapter objDa = new SqlDataAdapter();  
  7.             DataSet objDs = new DataSet();  
  8.   
  9.   
  10.             objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  11.             objCmd = new SqlCommand();  
  12.             objCmd.CommandText = "sp_WebToDoListSet";  
  13.             objCmd.CommandType = CommandType.StoredProcedure;  
  14.             objCmd.Connection = objConn;  
  15. }  
  16.   
  17. //Second Method  
  18.   public DataSet GetToDoList(int EmployeeId)  
  19.         {  
  20.             List<ProjectProp> ProjectList;  
  21.             ProjectList = new List<ProjectProp>();  
  22.   
  23.   
  24.             SqlConnection objConn = new SqlConnection();  
  25.             SqlCommand objCmd = new SqlCommand();  
  26.             SqlDataAdapter objDa = new SqlDataAdapter();  
  27.             DataSet objDs = new DataSet();  
  28.   
  29.             // EmpId = int.Parse(System.Web.HttpContext.Current.Session["EmployeeID"].ToString());  
  30.   
  31.             objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  32.             objCmd = new SqlCommand();  
  33.             objCmd.CommandText = "sp_GetToDoList";  
  34.             objCmd.CommandType = CommandType.StoredProcedure;  
  35.             objCmd.Connection = objConn;  
  36. }  
  37.   
  38. //Another Method  
  39.   public void DeleteToDoList(int ToDoListId)  
  40.         {  
  41.             SqlConnection objConn = new SqlConnection();  
  42.             SqlCommand objCmd = new SqlCommand();  
  43.             SqlDataAdapter objDa = new SqlDataAdapter();  
  44.             DataSet objDs = new DataSet();  
  45.   
  46.             objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  47.             objCmd = new SqlCommand();  
  48.             objCmd.CommandText = "sp_DeleteToDoList";  
  49.             objCmd.CommandType = CommandType.StoredProcedure;  
  50.             objCmd.Connection = objConn;  
  51. }  
 Any Help will be much appreciated!
 
Thanks 
 

Answers (2)