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
- //First Method
- public void InsertToDoList(ToDoListProp ToDoListProp)
- {
- SqlConnection objConn = new SqlConnection();
- SqlCommand objCmd = new SqlCommand();
- SqlDataAdapter objDa = new SqlDataAdapter();
- DataSet objDs = new DataSet();
- objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- objCmd = new SqlCommand();
- objCmd.CommandText = "sp_WebToDoListSet";
- objCmd.CommandType = CommandType.StoredProcedure;
- objCmd.Connection = objConn;
- }
- //Second Method
- public DataSet GetToDoList(int EmployeeId)
- {
- List
ProjectList; - ProjectList = new List
(); - SqlConnection objConn = new SqlConnection();
- SqlCommand objCmd = new SqlCommand();
- SqlDataAdapter objDa = new SqlDataAdapter();
- DataSet objDs = new DataSet();
- // EmpId = int.Parse(System.Web.HttpContext.Current.Session["EmployeeID"].ToString());
- objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- objCmd = new SqlCommand();
- objCmd.CommandText = "sp_GetToDoList";
- objCmd.CommandType = CommandType.StoredProcedure;
- objCmd.Connection = objConn;
- }
- //Another Method
- public void DeleteToDoList(int ToDoListId)
- {
- SqlConnection objConn = new SqlConnection();
- SqlCommand objCmd = new SqlCommand();
- SqlDataAdapter objDa = new SqlDataAdapter();
- DataSet objDs = new DataSet();
- objConn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- objCmd = new SqlCommand();
- objCmd.CommandText = "sp_DeleteToDoList";
- objCmd.CommandType = CommandType.StoredProcedure;
- objCmd.Connection = objConn;
- }
Thanks
Gajendra JangidPosted Mar 2, 2019, 6:36 AM
Salman BegPosted Mar 2, 2019, 3:47 AM