.net批量插入数据库,SqlBulkCopy方法

   /// <summary>

        /// 把数据插入LessonQuestion表

        /// </summary>

        /// <param name="lessontitle"></param>

        /// <param name="region"></param>

        public void InsertTable(int questionid,List<DE.Model.DE_LessonInfo> dless)

        {

            DataTable dtlessoninfo = new DataTable();

            dtlessoninfo.Columns.Add(new DataColumn("L_ID"));

            dtlessoninfo.Columns.Add(new DataColumn("Q_ID"));



                //遍历每一项,把各项加入表

                #region 批量插入数据到LessonInfo

            foreach (DE.Model.DE_LessonInfo ragion in dless)

                {

                    DataRow dr = dtlessoninfo.NewRow();

                    dr["L_ID"] = ragion.L_ID;

                    dr["Q_ID"] = questionid;

                    dtlessoninfo.Rows.Add(dr);

                }

                SqlConnection sq = new SqlConnection(DbHelperSQL.connectionString);

                using (sq)

                {

                    sq.Open();

                    SqlBulkCopy sbc = new SqlBulkCopy(sq);

                    sbc.ColumnMappings.Add("L_ID", "L_ID");

                    sbc.ColumnMappings.Add("Q_ID", "Q_ID");

                    sbc.DestinationTableName = "DE_LessonQuestion";

                    sbc.WriteToServer(dtlessoninfo);

                #endregion

                }

        } 

        

 

你可能感兴趣的:(.net)