Beispiel #1
0
        /// <summary>
        ///     使用SqlBulkCopy方式插入数据
        /// </summary>
        /// <returns></returns>
        private static void SqlBulkCopyInsert <T>(IList <T> list)
        {
            var ltt       = new ListToDataTable();
            var dataTable = ltt.ConvertTo(list);

            ltt.Dispose();
            var sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);

            sqlcon.Open();
            var sqlBulkCopy = new SqlBulkCopy(sqlcon);

            sqlBulkCopy.DestinationTableName = "tg_task";

            if (dataTable != null && dataTable.Rows.Count != 0)
            {
                sqlBulkCopy.ColumnMappings.Add("task_id", "task_id");
                sqlBulkCopy.ColumnMappings.Add("id", "id");
                sqlBulkCopy.ColumnMappings.Add("user_id", "user_id");
                sqlBulkCopy.ColumnMappings.Add("task_type", "task_type");
                sqlBulkCopy.ColumnMappings.Add("task_state", "task_state");
                sqlBulkCopy.ColumnMappings.Add("task_step_type", "task_step_type");
                sqlBulkCopy.ColumnMappings.Add("task_step_data", "task_step_data");
                sqlBulkCopy.ColumnMappings.Add("task_starttime", "task_starttime");
                sqlBulkCopy.ColumnMappings.Add("task_endtime", "task_endtime");
                sqlBulkCopy.ColumnMappings.Add("rid", "rid");
                sqlBulkCopy.ColumnMappings.Add("task_base_identify", "task_base_identify");
                sqlBulkCopy.ColumnMappings.Add("is_lock", "is_lock");
                sqlBulkCopy.ColumnMappings.Add("is_special", "is_special");
                sqlBulkCopy.ColumnMappings.Add("task_coolingtime", "task_coolingtime");
                sqlBulkCopy.WriteToServer(dataTable);
            }
            sqlBulkCopy.Close();
            sqlcon.Close();
            sqlcon.Dispose();
        }