-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.
Bro which negative people sense the post without even saying anything in the comments of a discouragement of fucking trying to learn something
– gnomo
Your tables have some form of Quence?
– Danizavtz
then and sequentially yes
– gnomo
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.
– Danizavtz
@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 layermodelagem
in the resources ofchave primária e chave estrangeira
. Try to be more specific where your difficulty is being.– MagicHat
From what I understand, you have a Master/Detail situation, where
emprestimo
is the master table anditem
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 theinsert
and use this new id to insert the records intoitens
.– Clarck Maciel
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.
– Clarck Maciel
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
– Augusto Vasques