Unreachable code Detected on bank connection

Asked

Viewed 35 times

2

Oops good night,

Personal to with a doubt that it is something silly, is that I am not accustomed to use an external connection class in the development, had this bad-habit but now I went to use and to find a Unreachable code if someone can help me/ explain why I appreciate :) Follows class and method error ta no con.disconnect();:

public class Conexao:IDisposable
{
    private static string stringConexao = ConfigurationManager.ConnectionStrings["stringConexao"].ConnectionString;

    private SqlConnection con = new SqlConnection(stringConexao);

    public void conectar()
    {
        if (con.State == System.Data.ConnectionState.Closed)
        {
            con.Open();
        }
    }

    public void desconectar()
    {
        if (con.State == System.Data.ConnectionState.Open)
        {
            con.Close();
        }
    }

    public SqlConnection getCon()
    {
        return con;
    }
}

public int cadastrarAssociado (Associado assoc)
    {
        using(Conexao con = new Conexao())
        {
            using(SqlCommand comando = new SqlCommand(procCadastrar, con.getCon()))
            {
                comando.CommandType = System.Data.CommandType.StoredProcedure;

                comando.Parameters.AddWithValue("@cpf", assoc.Cpf);
                comando.Parameters.AddWithValue("@nome", assoc.Nome);
                comando.Parameters.AddWithValue("@rg", assoc.Rg);
                comando.Parameters.AddWithValue("@data_nasc", assoc.Nascimento.Date);
                comando.Parameters.AddWithValue("@endereco", assoc.Endereco);
                comando.Parameters.AddWithValue("@apto", assoc.Apto);
                comando.Parameters.AddWithValue("@cidade", assoc.Cidade);
                comando.Parameters.AddWithValue("@estado", assoc.Estado);
                comando.Parameters.AddWithValue("@email", assoc.Email);
                comando.Parameters.AddWithValue("@usuario", assoc.Login);
                comando.Parameters.AddWithValue("@senha", assoc.Senha);

                try
                {
                    con.conectar();

                    return Convert.ToInt32(comando.ExecuteScalar());

                    con.desconectar();
                }
                catch (SqlException)
                {
                    throw;
                }
            }
        }
    }
  • Just remove the line! and its connection class has not been implemented Idisposable?

  • But if I remove the line the connection will not close, It was the Idisposable only for when you finish using the using take from memory

  • If you implement IDisposable and put the disconnect on it yes! because it is using using;

  • Really now I touched myself, I didn’t say it was something silly

  • put in the answer!

1 answer

3


Implements the interface Idisposabe as in the code below:

public class Conexao: IDisposable
{
    private static string stringConexao = ConfigurationManager.ConnectionStrings["stringConexao"].ConnectionString;

    private SqlConnection con = new SqlConnection(stringConexao);

    public void conectar()
    {
        if (con.State == System.Data.ConnectionState.Closed)
        {
            con.Open();
        }
    }

    public void desconectar()
    {
        if (con.State == System.Data.ConnectionState.Open)
        {
            con.Close();
        }
    }

    public SqlConnection getCon()
    {
        return con;
    }

    public void Dispose()
    {
        desconectar();
        GC.SuppressFinalize(this);
    }
}

Remove the line: con.desconectar();, how you used the using he will call the dispose in a transparent and automatic way of an object.

C# - Why use?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.