zoukankan      html  css  js  c++  java
  • 用.NET SqlBulkCopy类执行批量插入数据到数据库

    SqlBulkCopy类的属性和方法:

    属性DestinationTableName指定接受复制记录的表。

    方法 WriteToServer执行批量复制。它接受DataTableDataRowIDataReader对象为复制数据源。你还可以用DataTable对象包含一个DataRowState值,指定仅复制匹配的行。

    SqlBulkCopy类的主要作用:

    ·    单独批量复制操作,可将数据从一个数据源移动到SQL Server表中。

    ·    也可执行多个批量复制操作。

    ·    在数据库事务中可执行批量复制操作。

    使用实例:

    #region数据库对应表

            /// <summary>

            /// 数据库对应表

            /// </summary>

            /// <returns></returns>

            private static DataTable GetTable()

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("User_ID", typeof(string));

                dt.Columns.Add("User_Name", typeof(string));

                dt.Columns.Add("User_Birth", typeof(DateTime));

                dt.Columns.Add("User_Sex", typeof(int));

                return dt;

            }

            #endregion

     

            /// <summary>

            /// 批量插入数据

            /// </summary>

            /// <param name="context"></param>

            /// <returns></returns>

            public void AddModels()

            { List<UserEntity> users = (List< UserEntity>)HttpContext.Current.Cache["UserList "];         

                DataTable dt_users = GetTable ();

                foreach (UserEntity model in users)

                {

                    DataRow dr_user = dt_ users.NewRow();

                  

                        dr_ user ["User_ID"] = Guid.NewGuid().Tostring();

                        dr_ user ["User_Name "] = model.UserName;

                        dr_ user ["User_Birth "] = model.UserBirth;

                        dr_ user ["UserSex"] = model.Sex;

                        dt_ users.Rows.Add(dr_ user);

                    }

                }

                if (dt_ users != null && dt_ user.Rows.Count > 0)

                {

                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString))

                    {

                        conn.Open();

                        using (SqlTransaction trans = conn.BeginTransaction())

                        {

                            try

                            {

                                using (SqlBulkCopy bulk = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans))

                                {

                                    bulk.BatchSize = 1000000;

                                    bulk.DestinationTableName = "db_User";

                                    bulk.WriteToServer(dt_users);

                                    trans.Commit();

                                    IsRead = true;

                                }

                            }

                            catch

                            {

                                IsRead = false;

                                trans.Rollback();

                            }

                        }

                    }

                }

            }

  • 相关阅读:
    一道编程面试题
    AtCoder Grand Contest 016 B
    AtCoder Regular Contest 076 D
    Codeforces Round #419 (Div. 2) B. Karen and Coffee
    2017 计蒜之道 复赛 Windows画图+百度地图导航
    Codeforces Round #418 (Div. 2) B. An express train to reveries
    棠梨煎雪
    Bits And Pieces
    Let Them Slide
    TeaTree
  • 原文地址:https://www.cnblogs.com/Cynosure/p/2214577.html
Copyright © 2011-2022 走看看