在一个请求中插入多行c#

时间:2016-10-26 09:38:21

标签: c# .net-3.5 smart-device-framework

在visual studio 2008中我创建了一个智能设备网3.5,请我在一个请求中插入5000,例如“插入到group_user选择1,5 union all select 2,5 ......”但它没有工作,我直接在sqlmanagment尝试他准备插入,但有例外(插入许多值)和抱歉我的英语:(

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

0 个答案:

没有答案