Inserting data into two tables in a single form

Asked

Viewed 72 times

1

Hello, good morning, good morning! I am studying Webform in college, and I have a question to enter a value of a single form page for two Tables. In case to exemplify, I have two tables "Person" and "Client". The person table has the fields (pes_id, pes_name) and cli_client (cli_id, pes_id, pes_note). However I do not know how to enter this data, because I need logic to enter the person to have the id (because it is an FK) to be able to enter a client. Would two simultaneous INSERTS be in the Persistence class? I know, by internet search use the Executescalar() to get the last entered record of the person, but do not know how to register the client with this data. My code is like this in the Personal class p/ insert.

public static int Inserir(Pessoa pes)
{
    int retorno = 0;
    try
    {
        IDbConnection objConexao;
        IDbCommand objComando;
        objConexao = Mapped.Connection();
        string sql = "INSERT INTO pes_pessoa(pes_nome) VALUES (?pes_nome) ; SELECT LAST_INSERT_ID(); ";
        objComando = Mapped.Command(sql, objConexao);
        objComando.Parameters.Add(Mapped.Parameter("?pes_id", pes.Id));
        objComando.Parameters.Add(Mapped.Parameter("?pes_nome", pes.Nome));
        retorno = Convert.ToInt32(objComando.ExecuteScalar());

        objConexao.Close();
        objComando.Dispose();
        objConexao.Dispose();
    }
    catch (Exception e)
    {
        retorno = -2;
    }
    return retorno;
}

Clientedb

public static int Inserir(Cliente cli)
{
    int retorno = 0;
    try
    {
        IDbConnection objConexao;
        IDbCommand objComando;
        objConexao = Mapped.Connection();
        string sql = "INSERT INTO cli_cliente( cli_observacao, pes_id) VALUES ( ?cli_observacao, ?pes_id) ; SELECT LAST_INSERT_ID(); ";
        objComando = Mapped.Command(sql, objConexao);
        objComando.Parameters.Add(Mapped.Parameter("?cli_id", cli.Id));
        objComando.Parameters.Add(Mapped.Parameter("?cli_observacao", cli.Observacao));
        objComando.Parameters.Add(Mapped.Parameter("?pes_id", cli.Pessoa));
        retorno = Convert.ToInt32(objComando.ExecuteScalar());
        objConexao.Close();
        objComando.Dispose();
        objConexao.Dispose();
    }
    catch (Exception e)
    {
        retorno = -2;
    }
    return retorno;
}
  • 3

    I regret that I have been learning an abandoned technology for almost 15 years (I really regret that they are teaching specific technologies), and I hope that the college has not indicated to do the code like this, although it does not have some typical problems, there are others like the call of Dispose() or Close() instead of using the using, including the creation of a class that has a pattern that depends on layout without its correct implementation, capture Excepion, use error code indiscriminately, and DB column names are well tuins because they use Hungarian notation.

  • 2

    I can’t say 100%, but congratulations on treating Cliente as a role of Pessoa and not as if it were a Pessoa how everyone makes mistakes. Just watch out for research-based learning on the internet. Sometimes you find in it people saying that the Earth is flat. I know, you should laugh at it because you know it’s a lie. But on a subject you can’t master if the first one you see is a lie or a wrong thing that size you’ll learn wrong.

  • 2

    Of course, there’s a lot to be said, too, but you’re still not able to evaluate it when you’re learning. By the patterns that everybody does is just that, you insert one takes the code obtained and the other gets this id obtained before for use in what you need. I did not see a clear problem in your question. It can be done differently and can be an atomic form, ensuring that both are inserted at the same time with warranty.

  • 2

    This is not always necessary in this case because the Pessoa should be able to exist without the role of Cliente exist, and the addition of Cliente it should be possible to occur when the Pessoa it already exists, so it should have a logic to address that possibility. If I had understood the doubt maybe I would have an answer, I could still improve on that. Note that you are writing unnecessary code: https://answall.com/a/328008/101, https://answall.com/a/99820/101. This may have answered your question: https://answall.com/q/44648/101.

  • 2

    On the subject that I talked about problems, there is more information here on the site to better understand and the content was classified by several people, so the chance that it is right is great, research and analyze.

No answers

Browser other questions tagged

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