I have created a class file for DAL with 4 function which takes the charge of entire data handling required for the entire application.
Here is the code of that. I just want to know accessing this class file for all data needs will slowdown the application being in single class file ?
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections; using System.Data.SqlClient;
/// <summary> /// Summary description for General /// </summary>
namespace Generalns { public class General : System.Web.UI.Page { private SqlConnection erpsqlcon, GLconn; private SqlDataAdapter erpsqlda, GLda; private SqlCommand erpsqlcmd, GLcmd;
string valRet, GLFillRet;
DataSet erpsqlds = new DataSet(); DataSet GLds = new DataSet();
public string erpconstr() { return ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString; }
public string GLconstr() { return ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString; }
public DataSet GLGetData(string spname, int paracnt, string[,] paraarr) { int i = 0;
try { GLconn = new SqlConnection(ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString); GLda = new SqlDataAdapter(spname, GLconn);
GLda.SelectCommand.CommandType = CommandType.StoredProcedure; if (paracnt > 0) { for (i = 0; i < paracnt; i++) { SqlParameter tmppara = new SqlParameter(paraarr[i, 0].ToString(), SqlDbType.VarChar, 20); GLda.SelectCommand.Parameters.Add(tmppara); GLda.SelectCommand.Parameters[paraarr[i, 0].ToString()].Value = paraarr[i, 1].ToString(); } }
Whether you're using a single class or multiple classes, this has no major impact on the performance of your application but rather on the maintenance. It is always a best practise not to have duplicate code written in any of the layers of your application.
You have to know how to weigh your code between keeping it simple and easily maintainable.
In your case, make sure you have a dispose method in your class that you can call to release all resources that it owns from the memory.
Please Mark as Answered if this post helps you.Your 'Mark' will help other people having similar problem.
Also please obscure some of your code before pasting it here. Your company/client would regard their source code as highly confidential. It is not a good practice to copy - paste it verbatim.
erpsqlda = new SqlDataAdapter("SELECT " + fldname.ToString() + " FROM " + tblname.ToString() + " WHERE " + cond.ToString(), erpsqlcon);
One other piece of advice here: with this style of SQL concatenation in code you are setting yourself up to be prone to what is called SQL Injection Attacks. You are better off using a SQLCommand object, passing it the SQLCommandParameters, and then passing
that to the constructor of the SqlDataAdapter object. The following will help explain, and in fact the 1st link shows using a SqlDataAdapter in the way you are above as a way of
not to do things:
kganandh
Member
4 Points
4 Posts
Using a single class - Feasibility Status
Oct 20, 2011 07:48 AM|LINK
Hi There,
I have created a class file for DAL with 4 function which takes the charge of entire data handling required for the entire application.
Here is the code of that. I just want to know accessing this class file for all data needs will slowdown the application being in single class file ?
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Data.SqlClient;
/// <summary>
/// Summary description for General
/// </summary>
namespace Generalns
{
public class General : System.Web.UI.Page
{
private SqlConnection erpsqlcon, GLconn;
private SqlDataAdapter erpsqlda, GLda;
private SqlCommand erpsqlcmd, GLcmd;
string valRet, GLFillRet;
DataSet erpsqlds = new DataSet();
DataSet GLds = new DataSet();
public string erpconstr()
{
return ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString;
}
public string GLconstr()
{
return ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString;
}
public DataSet GLGetData(string spname, int paracnt, string[,] paraarr)
{
int i = 0;
try
{
GLconn = new SqlConnection(ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString);
GLda = new SqlDataAdapter(spname, GLconn);
GLda.SelectCommand.CommandType = CommandType.StoredProcedure;
if (paracnt > 0)
{
for (i = 0; i < paracnt; i++)
{
SqlParameter tmppara = new SqlParameter(paraarr[i, 0].ToString(), SqlDbType.VarChar, 20);
GLda.SelectCommand.Parameters.Add(tmppara);
GLda.SelectCommand.Parameters[paraarr[i, 0].ToString()].Value = paraarr[i, 1].ToString();
}
}
GLds = new DataSet();
GLda.Fill(GLds);
return GLds;
}
catch (Exception ex)
{
}
finally
{
GLconn.Close();
}
return GLds;
}
public DataSet ErpGetData(string tblname, string fldname, string cond)
{
try
{
erpsqlcon = new SqlConnection(erpconstr());
if (cond.Length > 0)
erpsqlda = new SqlDataAdapter("SELECT " + fldname.ToString() + " FROM " + tblname.ToString() + " WHERE " + cond.ToString(), erpsqlcon);
else
erpsqlda = new SqlDataAdapter("SELECT " + fldname.ToString() + " FROM " + tblname.ToString(), erpsqlcon);
erpsqlda.Fill(erpsqlds);
erpsqlcon.Close();
}
catch (Exception ex)
{
if (erpsqlcon.State == ConnectionState.Open)
{
erpsqlcon.Close();
}
}
finally
{
erpsqlcon.Close();
}
return erpsqlds;
}
public string GLFillData(string spname, int paracnt, string[,] paraarr)
{
int i = 0;
try
{
GLconn = new SqlConnection(ConfigurationManager.ConnectionStrings["GFMSData"].ConnectionString);
GLda = new SqlDataAdapter(spname, GLconn);
GLda.SelectCommand.CommandType = CommandType.StoredProcedure;
if (paracnt > 0)
{
for (i = 0; i < paracnt; i++)
{
SqlParameter tmppara = new SqlParameter(paraarr[i, 0].ToString(), SqlDbType.VarChar, 20);
GLda.SelectCommand.Parameters.Add(tmppara);
GLda.SelectCommand.Parameters[paraarr[i, 0].ToString()].Value = paraarr[i, 1].ToString();
}
}
GLds = new DataSet();
GLda.Fill(GLds);
//return GLds;
}
catch (Exception ex)
{
}
finally
{
GLconn.Close();
}
return GLFillRet;
}
public string ErpFillData(string ErpSqlStr)
{
try
{
erpsqlcon = new SqlConnection(erpconstr());
erpsqlcmd = new SqlCommand(ErpSqlStr, erpsqlcon);
erpsqlcmd.Connection.Open();
//erpsqlcmd.ExecuteNonQuery();
valRet = erpsqlcmd.ExecuteScalar().ToString();
erpsqlcmd.Connection.Close();
}
catch (Exception ex)
{
if (erpsqlcon.State == ConnectionState.Open)
{
erpsqlcon.Close();
}
}
finally
{
erpsqlcon.Close();
}
return valRet;
}
}
}
Expert guidelines is required on this part. Few lines of code has been removed. But still this will work.
This class file can interact with any stored procedure no matter how many parameters are there.
gsaadeh
Member
736 Points
141 Posts
Re: Using a single class - Feasibility Status
Oct 20, 2011 08:07 AM|LINK
Whether you're using a single class or multiple classes, this has no major impact on the performance of your application but rather on the maintenance. It is always a best practise not to have duplicate code written in any of the layers of your application. You have to know how to weigh your code between keeping it simple and easily maintainable.
In your case, make sure you have a dispose method in your class that you can call to release all resources that it owns from the memory.
Brutus Maxim...
Member
68 Points
15 Posts
Re: Using a single class - Feasibility Status
Oct 20, 2011 08:54 AM|LINK
Also please obscure some of your code before pasting it here. Your company/client would regard their source code as highly confidential. It is not a good practice to copy - paste it verbatim.
Brutus
kganandh
Member
4 Points
4 Posts
Re: Using a single class - Feasibility Status
Oct 21, 2011 06:05 AM|LINK
Hi there,
Thank you for the time for yor reply I will work on the things you defined.
kganandh
Member
4 Points
4 Posts
Re: Using a single class - Feasibility Status
Oct 21, 2011 06:07 AM|LINK
Hi Brutus,
I agree with the points you defined. This is my own product code even then I should do what you said. Thank you
neon2
Member
44 Points
48 Posts
Re: Using a single class - Feasibility Status
Oct 21, 2011 12:03 PM|LINK
Oppssss, is it a DAL of A DB helper Class ?
atconway
All-Star
16846 Points
2756 Posts
Re: Using a single class - Feasibility Status
Oct 26, 2011 02:26 PM|LINK
One other piece of advice here: with this style of SQL concatenation in code you are setting yourself up to be prone to what is called SQL Injection Attacks. You are better off using a SQLCommand object, passing it the SQLCommandParameters, and then passing that to the constructor of the SqlDataAdapter object. The following will help explain, and in fact the 1st link shows using a SqlDataAdapter in the way you are above as a way of not to do things:
How To: Protect From SQL Injection in ASP.NET:
http://msdn.microsoft.com/en-us/library/ms998271.aspx
SqlDataAdapter Constructor (SqlCommand):
http://msdn.microsoft.com/en-us/library/awzk4kc1.aspx
neon2
Member
44 Points
48 Posts
Re: Using a single class - Feasibility Status
Oct 28, 2011 11:33 AM|LINK
one more thing when u create only one instance of a class through out ur application , be care ful of concurency issue.
Happy Codding.