0
\$\begingroup\$

How could I improve this code?

public class DALUser
 {
 SqlConnection conn;
 SqlCommand cmd;
 public void ExecuteSp(string Spname, Dictionary<dynamic, dynamic> Params)
 {
 try
 {
 initialize();
 cmd = new SqlCommand();
 cmd.Connection = conn;
 cmd.CommandText = Spname;
 cmd.CommandType = CommandType.StoredProcedure;
 cmd = GetParam(Params, cmd);
 cmd.ExecuteNonQuery();
 }
 finally
 {
 DeIntialize();
 }
 }
 public DataTable ExecuteAdapter(string Spname, Dictionary<dynamic, dynamic> Params)
 {
 try
 {
 initialize();
 cmd = new SqlCommand();
 cmd.Connection = conn;
 DataTable dt = new DataTable();
 cmd.CommandText = Spname;
 cmd.CommandType = CommandType.StoredProcedure;
 cmd = GetParam(Params, cmd);
 SqlDataAdapter sda = new SqlDataAdapter(cmd);
 sda.Fill(dt);
 return dt;
 }
 finally
 {
 DeIntialize();
 }
 }
 SqlCommand GetParam(Dictionary<dynamic, dynamic> Params, SqlCommand cmd)
 {
 foreach (var objparam in Params)
 {
 cmd.Parameters.AddWithValue(Const.sParamPrefix + objparam.Key, objparam.Value);
 }
 return cmd;
 }
 #region SqlConnection Side
 void initialize()
 {
 if (CheckConnection())
 {
 conn = new SqlConnection(Const.ConnectionString);
 conn.Open();
 }
 }
 bool CheckConnection()
 {
 if (conn == null)
 return true;
 return false;
 }
 void DeIntialize()
 {
 if (!CheckConnection())
 {
 conn.Close();
 conn.Dispose();
 }
 }
 #endregion
 }
RubberDuck
31.1k6 gold badges73 silver badges176 bronze badges
asked Sep 11, 2014 at 14:23
\$\endgroup\$
3
  • 5
    \$\begingroup\$ Welcome to CodeReview. Could you please add some textual context regarding your code and questions? \$\endgroup\$ Commented Sep 11, 2014 at 14:32
  • \$\begingroup\$ We would like you to tell us what your code does, so we can see if something is wrong within it. \$\endgroup\$ Commented Sep 18, 2014 at 14:18
  • \$\begingroup\$ It's completely beside the point of the review, but if you're working with SP-heavy database, maybe you should look into one of the micro-ORMs that makes it very easy to call stored procedures, for example Insight.Database, instead of rolling out your own solution. \$\endgroup\$ Commented Sep 18, 2014 at 15:00

1 Answer 1

3
\$\begingroup\$

Your initialize and DeInitialize do the job of connecting and disconnecting your SQL Connection, but not the SQL Command that you create in each method.

I am sure that closing and disposing the connection will take care of the Command but I like to make sure.

so what I would do is to use a using statement

initialize();
using (SqlCommand cmd = new SqlCommand() { 
 .Connection = conn,
 .CommandText = Spname,
 .CommandType = CommandType.StoredProcedure
})
{
 cmd = GetParam(Params, cmd);
 cmd.ExecuteNonQuery();
}

your try/catch works, but I would rather have my connection opened and closed inside each method and not have to call 2 other methods and a try/catch to make it work. I would much rather just do this

using (SqlConnection conn = new SqlConnection(Const.ConnectionString))
using (SqlCommand cmd = new SqlCommand() { 
 .Connection = conn,
 .CommandText = Spname,
 .CommandType = CommandType.StoredProcedure
})
{
 cmd = GetParam(Params, cmd);
 cmd.ExecuteNonQuery();
}

and that gets rid of the try catch and all the ugly close/dispose calls.

It also gets rid of the need for a check connection method because when you use a using statement there is no open connection outside of the using block.


And OCD tells me to change this as well

public DataTable ExecuteAdapter(string Spname, Dictionary<dynamic, dynamic> Params)
{
 try
 {
 initialize();
 cmd = new SqlCommand();
 cmd.Connection = conn;
 DataTable dt = new DataTable();
 cmd.CommandText = Spname;
 cmd.CommandType = CommandType.StoredProcedure;
 cmd = GetParam(Params, cmd);
 SqlDataAdapter sda = new SqlDataAdapter(cmd);
 sda.Fill(dt);
 return dt;
 }
 finally
 {
 DeIntialize();
 }
}

and make it look like this instead

public DataTable ExecuteAdapter(string Spname, Dictionary<dynamic, dynamic> Params)
{
 using (SqlCommand cmd = New SqlCommand(){
 .Connection = conn,
 .CommandText = Spname,
 .CommandType = CommandType.StoredProcedure
 })
 {
 cmd = GetParam(Params,cmd);
 using (DataTable dt = new DataTable())
 using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
 {
 sda.Fill(dt);
 return dt;
 }
 }
}

Naming

initialize();

should be capitalized for standards sake

Initialize();

Method names are PascalCase
variables are camelCase

I know this probably isn't the real SPROC name but Spname should be spName even though Hungarian name scheme is no longer a recommended name scheme.

answered Sep 18, 2014 at 14:03
\$\endgroup\$
4
  • \$\begingroup\$ Is spName Hungarian style? Presumably it's just a shortening of storedProcedureName \$\endgroup\$ Commented Sep 18, 2014 at 14:20
  • \$\begingroup\$ @BenAaronson, I always thought that Hungarian was type abbreviation and then a name for the variable like strName, and now I see your point, because Stored Procedure isn't the type of that variable it's actually a string. something to think about for sure \$\endgroup\$ Commented Sep 18, 2014 at 14:24
  • 1
    \$\begingroup\$ I would say spName is Hungarian done right. If I started concatenating it with other strings, it would look wrong. \$\endgroup\$ Commented Sep 18, 2014 at 15:36
  • \$\begingroup\$ @RubberDuck I agree with you, it makes perfect sense the way you put it. \$\endgroup\$ Commented Sep 18, 2014 at 15:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.