Create aprés update avec condition!

abbeilleee Messages postés 43 Date d'inscription dimanche 3 novembre 2013 Statut Membre Dernière intervention 9 février 2014 - 17 nov. 2013 à 05:14
abbeilleee Messages postés 43 Date d'inscription dimanche 3 novembre 2013 Statut Membre Dernière intervention 9 février 2014 - 17 nov. 2013 à 18:23
Bonjour,
j'ai une table arbres avec les champs, idpos, position1,position2,position3,position4,position5,position6,position7,
alors, je dois parcourir ma table au début, si l'idpos que j'ai ajouter n'es pas dans la table, il va ajouter l'id avec la premiére position(position1), sinon, (si idpos choisis est déja dans la table, est position 1 pleine)il ajoute la position dans la position 2(la position contient un code que je dois saisir sur le formulaire)...jusqu'a la position 6, dans ce cas il va ajouter dans la position 7 aprés il va supprimer la premiere position1 et tout le tableu va se décaler et il resteras la position 7 vide , alors, j'ai opté a choisir un dataset pour parcourir les champs, aprés un insert si l'id n'es pas la, sinon, update, j'ajoute dans la derniére position vide!voila mon code, l'insert marche mais pas l'update, merci de vouloir corriger mon code! Merci de bien vouloir m'aider

public void dataset() 
{
            try{
                
                int i= 2;
                Boolean b = true;
                SqlConnection con = new SqlConnection("Data Source = \ \ SQLEXPRESS ; AttachDbFilename = C:. \ \ Users \ \ \ abbeillee \ Documents \ \ KBMA.mdf ; Integrated Security = True; Connect Timeout = 30; User Instance = True ");SqlDataAdapter adaptation= new SqlDataAdapter();
                adap.SelectCommand = new SqlCommand("select * from arbres ", Program.con);
                
                adap.Fill(x, "arbres");
                if (x.Tables["arbres"].Rows.Count == 0)
                {
                    string req1 = "insert into arbres(idpos, position1) values ('" + textBox1.Text + "','" + textBox2.Text + "');";
                    con.Open();
                    comm = new SqlCommand(req1, con);
                    comm.ExecuteNonQuery();
                    
                    MessageBox.Show("insertion bien faite ");
                }
                else
                {
                    while ((i <= 7) && (b == true))
                    {
                        string req2 = "select position"+i +" from arbres where position='"+i+"' is not null";
                        if (x.Tables[0].Rows.Count == 0){string req1 = "update arbres set position"+i+" = " + textBox2.Text + " where position"+i+"=position"+i;
                            con.Open();
                            comm = new SqlCommand(req1, con);
                            comm.ExecuteNonQuery();
                            b = false;
                            MessageBox.Show(req1);
                            MessageBox.Show("Modification bien faite");
                        }
                        i++;}

                }
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }




        }

2 réponses

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
17 nov. 2013 à 12:26
Tu as testé ce que je t'ai répondu dans un de tes autres tipics ??
0
abbeilleee Messages postés 43 Date d'inscription dimanche 3 novembre 2013 Statut Membre Dernière intervention 9 février 2014
17 nov. 2013 à 18:23
oui, j'ai essayé ca n'a pas marché il ajoute qu'un seul enregistrement, et c'est tout ! j'ai utilisé ce code, mais il ajoute un enregistrement en ajoutant deux position c'est tout!
 public void dataset()
        {
            try
            {
                
                int i = 2;
                Boolean b = true;
                SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\hachim\\Documents\tablearbres.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                
                SqlDataAdapter adap = new SqlDataAdapter();
                adap.SelectCommand = new SqlCommand("select * from arbres ", Program.con);
                
                adap.Fill(x,"arbres");
                if (x.Tables["arbres"].Rows.Count == 0)
                {
                    string req1 = "insert into arbres(idpos, position1) values ('" + textBox1.Text + "','" + textBox2.Text + "');";
                    con.Open();
                    comm = new SqlCommand(req1, con);
                    comm.ExecuteNonQuery();
                    
                    MessageBox.Show("insertion bien faite ");

                }
                else
                {
                    while ((i <= 7) && (b == true))
                    {
                        string req2 = "select position"+i +" from arbres where position='"+i+"' is  null ";
                        if (x.Tables["arbres"].Rows.Count != 0)
                        {

                            int dernierLigne = x.Tables["arbres"].Rows.Count-1;
                       //     string req1 = "update arbres set position"+i+" = " + textBox2.Text + " where position"+i+"=position"+i;
                            string req1 = "update arbres set position"+i+" = '" + textBox2.Text + "' where idpos like '" + x.Tables["arbres"].Rows[dernierLigne][0].ToString() + "'";
                            con.Open();
                       //       comm = new SqlCommand(req2, con);
                            comm = new SqlCommand(req1, con);
                            comm.ExecuteNonQuery();
                            b = false;
                            MessageBox.Show(req1);
                            MessageBox.Show("Modification bien faite");
                        }
                        i++;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }




        }
0
Rejoignez-nous