I have more than one function which simply fetches data from DB. The difference among the function is the stored procedure name (uspLoadStudents,uspLoadMarks). To optimize, make it as one function and passes the SP.
public DataSet LoadSubjects()
{
string SqlDBConnection = Utils.GetConnectionString();
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection(SqlDBConnection);
SqlCommand sqlCmd = new SqlCommand("uspLoadSubjects", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(sqlCmd.ExecuteReader());
ds.Tables.Add(dt);
sqlConn.Close();
return ds;
}
Information like sql command, stored procedure name, should be part of your
Data Access Layer, instead it is a helper class inside the data access layer. Try this:Implement a method to use this helper, for sample: