Insert to foreign SQL keys

Asked

Viewed 408 times

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();

1 answer

1

See the possibility to create a proc, I see is inserted data in the 3 tables of a single time, so you could do as follows...

Create proc with the necessary parameters

create Proc  Cliente_Orcamento  
(
  @nome nvarchar(50),
  @endereco nvarchar(50),
  @nomeProd  nvarchar(50),
  @precoProd numeric(18,2), 
  @data datetime,
  @IdCliente_Out int output
)
as
begin

  Set Xact_Abort on
  Set Nocount on
  Begin Tran

  begin
    INSERT into Cliente(nome, endereco) Values (@nome, @endereco)
    Set @IdCliente_Out = @@IDENTITY -- aqui vc pegar o id do cliente do
    -- ultimo insert.. linha executada acima    

    INSERT into Produto(nome, preco) Values (@nomeProd, @precoProd)
    INSERT into Orcamento(id_cliente) Values (@IdCliente_Out)
  end
  Commit
end

Call it that

connect.Open();

    OleDbCommand cmd1= new OleDbCommand("Cliente_Orcamento", connect); 

    if(connect.State == ConnectionState.Open)
    {
        cmd1.Parameters.Add("@nome",OleDbType.Char,20).Value = nome;
        cmd1.Parameters.Add("@endereco",OleDbType.Char,20).Value = endereco;
        cmd1.Parameters.Add("@nomeProd", OleDbType.Char, 20).Value = nomeProd;
        cmd1.Parameters.Add("@precoProd", OleDbType.Char, 20).Value = precoProd;
        cmd1.Parameters.Add("@data", OleDbType.Char, 20).Value = data;
        try 
        {           
            cmd1.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.

  • 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, oh yes on Msaccess has no support for Strored Procedures , thought it was sql server. an alternative to this would be vc after running your cmd1 select SELECT MAX(id) FROM Cliente and amaze the return value for passes in the cmd3.Parameters.

  • 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.

Browser other questions tagged

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