• C# List数据批量更新


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

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

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

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

    2、重点说下下面这种方式:通过XML的方式在数据库进行批量的更新

         1、建立ListToXML方法

              /// <summary>         

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

             /// </summary>        

            /// <returns></returns>         

          public static XmlDocument ListToXML<T>(string XmlName,IList<T> IL)         

           {             

                       try             

                           {                 

                             XmlDocument XMLdoc = new XmlDocument();                 

                             //建立XML的定义声明                 

                             XmlDeclaration XMLdec = XMLdoc.CreateXmlDeclaration("1.0", "utf-8", null);                 

                             XMLdoc.AppendChild(XMLdec);                 

                             XmlElement Root = XMLdoc.CreateElement(XmlName);                                               

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

                             foreach (T item in IL)                 

                             {                     

                                    XmlElement ChildNode = XMLdoc.CreateElement(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);                 

                           }                 

                      XMLdoc.AppendChild(Root);                 

                      return XMLdoc;             

                    }             

                   catch(Exception ex)             

                   {                 

                      //LogHelper.LogDebug("List<T>生成XML失败:" + ex.Message);                 

                      return null;             

                    }         

                }

         2、将写好的XMl直接当作参数@Data传入存储过程

               public  int UpdateAdminUsers(IList<AdminUserInfo> adminUsers)         

               {             

                         SqlParameter[] param = new SqlParameter[1];             

                         //拼接xml             

                        string data = XMLHelper.ListToXML<AdminUserInfo>("AdminUserList", adminUsers).InnerXml.Replace("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、数据库的分析XMl然后进行数据的处理

                Create PROCEDURE [dbo].[USP_AdminUsersUpdate]
           @Data XML
             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

            这种更新方式是将我们数据写成XML有效避免了参数无限次的输入

  • 相关阅读:
    Kafka API: TopicMetadata
    从事件总线和消息队列说起
    Object.defineproperty实现数据和视图的联动
    css3动画-animation
    css3动画-transition
    jquery判断对象的type
    vs如何在运行iis express调试时,不开打新窗口和关闭调试时,iis express不退出
    重写Equals的方式
    Android中包名不能大写
    C# 几种常见数据结构【转】
  • 原文地址:https://www.cnblogs.com/kangao/p/4911119.html
Copyright © 2020-2023  润新知