1
I am learning a programming language and I set out to develop a budget program. My first obstacle is to add the values of the primary keys in the foreign keys of the respective tables. This code below works well for this, but after copying the PK from the Client table to FK from the Budget table, the next data is always added to the following rows of the table and not to the current row as it should be. Can someone help me? Follow an excerpt from my code:
connect.Open();
OleDbCommand cmd1= new OleDbCommand("INSERT into Cliente(nome, endereco)"+"Values (@nome, @endereco)", connect); // nome e endereço
OleDbCommand cmd2 = new OleDbCommand("INSERT into Produto(nome, preco)" + "Values (@nomeProd, @precoProd)", connect); //produto, preço
OleDbCommand cmd3 = new OleDbCommand("INSERT into Orcamento(id_cliente) SELECT MAX(id) FROM Cliente", connect);
//Como eu faria para adicionar a informação "data" que está numa string no objeto acima, como as demais (cmd1 e cmd2) tendo em vista que existe o comando SELECT?
if(connect.State == ConnectionState.Open)
{
cmd1.Parameters.Add("@nome",OleDbType.Char,20).Value = nome;
cmd1.Parameters.Add("@endereco",OleDbType.Char,20).Value = endereco;
cmd2.Parameters.Add("@nome", OleDbType.Char, 20).Value = nomeProd;
cmd2.Parameters.Add("@preco", OleDbType.Char, 20).Value = precoProd;
cmd3.Parameters.Add("@id_cliente", OleDbType.Char, 20).Value = "";
cmd3.Parameters.Add("@data", OleDbType.Char, 20).Value = data;
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
I’m going to study this proc. I still don’t know how it works. Does the upper textbox code go in my class as a procedure? Thanks for your help so fast.
– Vanderlei
I couldn’t figure out how to do this (Strored Procedures) in the database created by Access. I can only use the update command to update foreign keys?
– Vanderlei
@Vanderlei, oh yes on Msaccess has no support for
Strored Procedures
, thought it was sql server. an alternative to this would be vc after running yourcmd1
selectSELECT MAX(id) FROM Cliente
and amaze the return value for passes in thecmd3.Parameters
.– Marco Souza
The problem is that cmd3 already does this, but when I create a cmd4 object that will work on the same line as cmd3, INSERT makes the @data information go to the next line. I still can’t figure out why this happens. I will try to use cmd3 to pass the date and in cmd4 try to use UPDATE FROM SELECT. Thanks for the support.
– Vanderlei