INSERT in batch c#

Asked

Viewed 459 times

1

I would like to know if it is possible and how to perform an INSERT in the database of 10 equal values and adding 1 more in a field..

ex: I have an equity system and I need to register 10 identical items. when marking a checkbox and adding the amount of items in a textbox he adds the 10 items to the database...

need that when recording is add +1 to the number of the platelet that is will be a random number typed..

ex:

txtPlaqueta.Text = "123"
CheckBox = true;
txtValorRepetir = "10" 

then I’d be in the comics like this

123,124,125,126,127,128,129,130,131,132

if I change the platelet number to "50" it would be in the comic

50,51,52,53,54,55,56,57,58,59

private void btAdd_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(//string de conexao);

        txtValor.Text = (int.Parse(txtValor.Text) + 1).ToString();

        string inserir = @"INSERT INTO DadosNome (Nome, Valor, Teste, Deletar) Values ('"+txtNome.Text+"', '"+txtValor.Text+"', '"+txtTeste.Text+"', '"+txtDeletar.Text+ "'),('" + txtNome.Text + "', '" + txtValor.Text + "', '" + txtTeste.Text + "', '" + txtDeletar.Text + "')";
        SqlCommand cmd = new SqlCommand(inserir, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

Assuming that the value column has value "22" when making the Index it would be (22,23,24,25) four records in the bd.

  • 1

    just make a loop... but put the code you use to make a link

  • Could you show me some code, so I know this loop.

1 answer

1


There are several problems in your code, but I will focus on the loop, and although with an impractical practice (concatenate the query string) I will show an example of how your question would look with the code you have:

    SqlConnection con = new SqlConnection(//string de conexao);


    int inicio = 22; //número do patrimonio inicial
    int quantidade = 10; //quantidade de números que serão inseridos

    string sqlInsert = @"INSERT INTO DadosNome (Nome, Valor, Teste, Deletar) Values ";

    for (int i = 0; i <= quantidade; i++)
    {
          sqlInsert += "('"+txtNome.Text+"', '"+ (inicio + i)  +"', '"+txtTeste.Text+"', '"+txtDeletar.Text+ "'),"; //vai concatenando a sintaxe dos values
    }

    sqlInsert = sqlInsert.Remove(sqlInsert.Lenght-1)+";"; //Remove a ultima virgula

    SqlCommand cmd = new SqlCommand(sqlInsert, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

As I mentioned earlier, I have not revised the code by altering some existing problems. I recommend reading about using the parameters when using an Sqlcommand: https://msdn.microsoft.com/pt-br/library/system.data.sqlclient.sqlparameter(v=vs.110). aspx

http://www.macoratti.net/09/07/c_adn_7.htm

In addition, you have to deal with mistakes that can happen

  • 1

    @Ronavannlinhalis, also has no impact on that particular scenario, but if memory serves, there is a limit of 1000 Values for each command of INSERT

  • Thank you @Rovannlinhalis, I was able to adapt to my code was right. I tried for days and it didn’t work...

  • @dionebravo do not forget to mark as reply. Thank you

Browser other questions tagged

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