How to insert multiple rows into a sql server, keeping all these rows in the same id

Asked

Viewed 52 times

-4

I am studying a program that lends equipment, in its proper functioning, when making a loan of material should be kept in the dbo.emprestimo all loan details (name, location, business, etc), then update dbo.equipamento to make that equipment "unavailable", and finally add all borrowed equipment in a third table dbo.item, where all borrowed equipment would be stored followed by the idEmprestimo for future consultation (similar to a supermarket purchase in which has the code of each purchase saved and within the code of the purchase has all products purchased).

The part about saving dbo.emprestimo and update the dbo.equipamento I am managing to do without problems, but the last part of saving the equipment in a third table dbo.item with the same loan id. I’m having a lot of trouble, I’ve been a few months idle and I’m a little rusty, I thought of something like:

code inside save loan button:

for (int i = 0; i < dataGridEmprestimo.Rows.Count; i++)
{
    SqlConnection con = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=Ianes;Integrated Security=True");
con.Open();

    SqlCommand cmd = new SqlCommand(@"INSERT INTO item (idEquipamento,idEmprestimo) VALUES ('" + dataGridEmprestimo.Rows[i].Cells[0].Value + "','" + HERE + "')", con);
    SqlCommand cmds = new SqlCommand(@"UPDATE equipamento SET equipamento.status = 'Indisponível' WHERE equipamento.id = '" + dataGridEmprestimo.Rows[i].Cells[0].Value + "'", con);

    cmd.ExecuteNonQuery();
    cmds.ExecuteNonQuery();
    con.Close();
}
            

Where the value "HERE" should be the value of the loan id, I thought of something like:

SELECT max(id) emprestimo

But I’m not getting a good result, I hope you understand.

  • 1

    Bro which negative people sense the post without even saying anything in the comments of a discouragement of fucking trying to learn something

  • Your tables have some form of Quence?

  • then and sequentially yes

  • I don’t know the implementation details of sqlserver, but I would go the way of using Sequence to keep the same id on all lines. I can share an example using the postgres of an implementation I did, but maybe it would be the same idea, would just adapt to your example.

  • 2

    @gnome generally negatives are indicators that something needs to be improved. While you describe well what needs to be done globally, the information about the difficulty itself is vague. " I am not having a good result", it would need to be replaced by, resultado_esperado, resultado_obtido, dificuldade, something like... Apparently your question is directly linked to logic we relationships between tables, in the abstraction layer modelagem in the resources of chave primária e chave estrangeira. Try to be more specific where your difficulty is being.

  • From what I understand, you have a Master/Detail situation, where emprestimo is the master table and item is the detail. Edit your question and insert the structure of these two tables, try to improve your question so that people understand easily so that it is easy to helpit makes the question more general so that it can help other people who will be with the same difficulty in the future. I don’t like the negative without comment, not the negative. I understood that you need to get the loan ID at the time of the insert and use this new id to insert the records into itens.

  • Please refer to this question: https://answall.com/questions/99816/insertindo-registro-e-recuperando-generatedid-bysql-server-2012. Please let us know if you helped resolve your problem.

  • The question code has more problems than the question suggests, apparently the two queries shown, INSERT and UPDATE, are subject to SQL injection. This in an application opens a gap so that a system user with few privileges can take control of the database. See Microsoft Docs - SQL injection and Sqlcommand.Prepare

Show 3 more comments
No answers

Browser other questions tagged

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