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
}