0

In visual studio 2008 I create a smart device net 3.5, Please I want to insert 5000 in one request ,for example "insert into group_user select 1,5 union all select 2,5 ..." but it doesn't work ,I tried directly in sqlmanagment he ready inserted but with exception (le nombre de valeurs dans la liste de sélection de sous-requête est insuffisant. insert many values ) and Sorry for my english :(

public String addGroupUser(List<GroupUser> ListGroupUser)
        {
            String concatenation = "";
            for (int i = 0; i < ListGroupUser.Count; i++)

            {

                int nbreEnreg = ListGroupUser.Count;
                int idGroup = ListGroupUser[i].group_id;
                int idUser = ListGroupUser[i].user_id;
                int dernierElement = nbreEnreg - 1;
               if (i == dernierElement)
                {
                    concatenation += " SELECT " + idUser + " ," + idGroup + "  ";

                }

                else
                {
                    concatenation += " SELECT " + idUser + " ," + idGroup + " UNION ALL  ";
                }
            }
            String requete = "insert into group_user  " + concatenation;

            return requete;
        }
internal static int ExecuteUpdateN(string requête,SqlCeConnection conne)
        {
            // on gère les éventuelles exceptions
            try
            {

                // exécute sqlCommand avec requête de mise à jour

                SqlCeCommand sqlCommand = new SqlCeCommand(requête, conne);

                int nbLignes = sqlCommand.ExecuteNonQuery();



                return nbLignes;

            }
            catch (Exception e)
            {

                return -1;
            }
            finally
            {
                Connexion.getInstance().closeConnection();
            }
        }

ExecuteUpdateN(requete);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What are the columns in your table `group_user`? I guess there are more than 2? – rene Oct 26 '16 at 09:45
  • You may have to specify the column names in parentheses after the table name - see this http://stackoverflow.com/questions/2624713/how-do-i-insert-multiple-rows-without-repeating-the-insert-into-dbo-blah-part – PaulF Oct 26 '16 at 09:47
  • No I have just 2 columns (int id_user, int id_group) – achraf bemrah Oct 26 '16 at 09:47
  • I tried with specify the columns of table but the same problems – achraf bemrah Oct 26 '16 at 09:51
  • Please debug your code and post the exact sql string which you try to run. – Sebi Oct 26 '16 at 10:08
  • Sebi I cant post it because it exceeded the 11700 caracters , Itired the sql String in sql managment I havent probleme (succes) – achraf bemrah Oct 26 '16 at 10:14
  • It like insert into group_user (id_user,id_group) SELECT 514 ,3 UNION ALL SELECT 515 ,4 UNION ALL SELECT 1 ,254 UNION ALL SELECT 2 ,254 UNION ALL . . . SELECT 6 ,254 UNION – achraf bemrah Oct 26 '16 at 10:19

0 Answers0