你的位置:首页 > ASP.net教程

[ASP.net教程]C# List数据批量更新


针对单条数据一般都是update语句直接更新

例如:update UserTable set UserName='小名'   where userid=xxx

但是如果是针对List数据组进行更新的话不外乎两种

1、程序for、foreach、while循环语句然后多次请求数据库更新(这种在这里不多做解释,因为基本上都知道这种方法)

2、重点说下下面这种方式:通过

     1、建立ListTo

          /// <summary>         

         /// 使用反射把List<T>转换成

         /// </summary>        

        /// <returns></returns>         

      public static string IList<T> IL)         

       {             

                   try             

                       {                 

                         new 

                         //建立

                         "1.0", "utf-8", null);                 

                        

                                                       

                         PropertyInfo[] PropertyInfos = typeof(T).GetProperties();                 

                         foreach (T item in IL)                 

                         {                     

                                typeof(T).Name);                                                 

                                foreach (PropertyInfo pro in PropertyInfos)                     

                                {                         

                                     if (pro != null)                         

                                       {                             

                                             string KeyName = pro.Name;                             

                                             string KeyValue = string.Empty;                             

                                             if (pro.GetValue(item, null) != null)                             

                                                 {                                 

                                                     KeyValue = pro.GetValue(item, null).ToString();                             

                                                  }                           

                                                 ChildNode.SetAttribute(KeyName,KeyValue);                            

                                                  ChildNode.InnerText = KeyValue;

                                           }

                               }                     

                            Root.AppendChild(ChildNode);                 

                       }                 

                 

                  return 

                }             

               catch(Exception ex)             

               {                 

                  //LogHelper.LogDebug("List<T>生成

                  return null;             

                }         

            }

     2、将写好的

           public  int UpdateAdminUsers(IList<AdminUserInfo> adminUsers)         

           {             

                     SqlParameter[] param = new SqlParameter[1];             

                     //拼接

                    string data = AdminUserInfo>("AdminUserList", adminUsers).Inner"encoding=\"utf-8\"", "");             

                    param[0] = new SqlParameter("@Data", data);             

                    object o = SqlHelper.ExecuteScalar(DataHelper.ConnectionString, CommandType.StoredProcedure, "USP_AdminUsersUpdate", param);             

                    return Convert.ToInt32(o);         

             }

    3、数据库的分析

            Create PROCEDURE [dbo].[USP_AdminUsersUpdate]
       @Data          AS
         BEGIN

         SET XACT_ABORT ON;
     BEGIN TRANSACTION
            DECLARE @RowCount AS INT=0;
        DECLARE @AdminUserList TABLE(
         [UserId] [int] NOT NULL,
             [UserName] [nvarchar](50) NULL
          );
        
        
         INSERT @AdminUserList (UserId ,UserName )
             SELECT T.c.value('@UserId','int') as UserId,
             T.c.value('@UserName','nvarchar(50)') as UserName
             FROM @Data.nodes('AdminUserList/AdminUserInfo') T(c);
        
         INSERT dbo.AdminUser ( UserId , UserName  )
            SELECT  UserId ,UserName FROM @AdminUserList;

            SET @RowCount=@@ROWCOUNT;

        COMMIT TRANSACTION
    SELECT @RowCount AS RowsCount;
           END

        这种更新方式是将我们数据写成