Unreachable code detected on connection to bank

2

Oops, good evening,

I'm not sure what to do, but I'm not sure what to do, but I'm not sure what to do. explain why I thank :) Follow class and method the error is not 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;
                }
            }
        }
    }
    
asked by anonymous 31.05.2016 / 03:24

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(); , as you used using it will call the dispose in an automatic and transparent way of an object.

C # - Why use using?

    
31.05.2016 / 03:43