码迷,mamicode.com
首页 > Web开发 > 详细

.NetCore3.1读取一个文件夹下面的所有文件资源并重新命名以及大批量更新及写入数据

时间:2020-02-01 14:33:12      阅读:809      评论:0      收藏:0      [点我收藏+]

标签:exists   clear   高效   message   database   HERE   environ   core   context   

using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCoreDemo.Controllers
{
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.FileProviders;

    public class UpdateGalleryTypeController : Controller
    {
        private IWebHostEnvironment _env;
        private IFileProvider _fileProvider;
        public UpdateGalleryTypeController(IWebHostEnvironment env, IFileProvider fileProvider)
        {
            this._env = env; this._fileProvider = fileProvider;
        }
        public IActionResult Index()
        {
            string msg = "";
            //DoUpdateBigData(out msg);//--大批量更新数据
            //DoBigDataInsert(out msg);//-------大数据批量写入
            string imgpath = Path.Combine(_env.WebRootPath, "smallimgs");
            string toFilePath = Path.Combine(_env.WebRootPath, "newFilePath");
            DirectoryInfo direcinfo = new DirectoryInfo(imgpath);
            if (direcinfo != null && direcinfo.Exists)
            {
                int index = 1100000;
                foreach (var item in direcinfo.GetFiles())
                {
                    if (item is FileInfo)
                    {
                        index += 1;
                        string fileExStr = Path.GetExtension(item.Name);
                        string newfilePath = Path.Combine(toFilePath, index + fileExStr);
                        System.IO.File.Copy(item.FullName, newfilePath);//---读取一个文件夹下面的所有文件并命名
                    }
                }
            }

            ViewBag.result = msg;
            return View();

        }

//*---通常我们平时需要一些比较大的测试数据,一次性写入,或者写入到队列在持久化到数据库,感觉写入的效率差强人意(刷新数据库只有几十几十的新增) //-----大批量的写入数据,测试中发现平均一秒写入的速度可达 400-500多条数据的样子,应该还有更高效的方法,欢迎大家给出宝贵的建议! private void DoBigDataInsert(out string msg) { try { Stopwatch watch = new Stopwatch(); watch.Start(); string dbconnectionStr = "server=****;uid=fengge;pwd=qq88;port=3306;database=BigDataTest;sslmode=none;"; StringBuilder sb = new StringBuilder(); int index = 0; for (int i = 0; i < 1000000; i++) { sb.Append("insert into Person(id,name,age)values(‘" + Guid.NewGuid().ToString() + "‘,‘name_" + new Random().Next(1, 99999) + "‘,‘" + new Random().Next(12, 38) + "‘);"); index = index + 1; if (index % 500 == 0) { Task.Run(() => DoWork(sb.ToString(), dbconnectionStr)).Wait(); sb.Clear(); } } if (sb != null && sb.Length > 0) { DoWork(sb.ToString(), dbconnectionStr); } msg = "ok"; watch.Stop(); ViewBag.totalTime = watch.Elapsed.TotalSeconds; } catch (Exception ex) { msg = ex.Message; } }

     //---比较高效一点的大批量修改数据
     //--应该还有更高效的方法,欢迎大家指出,谢谢
private void DoUpdateBigData(out string msg) { try { string dbconnectionStr = "server=aaaa;uid=QQ;pwd=aa222;port=3306;database=zrfDb;sslmode=none;"; using (yiyuneduContext db = new yiyuneduContext()) { var list = db.Gallery.Where(c => c.Gallerytype == null).Select(c => new { c.Galleryid, c.Gallerycontent }); StringBuilder sb = new StringBuilder(); int index = 0; list.ToList().ForEach(c => { var id = c.Galleryid; string _type = c.Gallerycontent.Substring(c.Gallerycontent.LastIndexOf(.) + 1); sb.Append("update gallery set gallerytype=‘" + _type + "‘ where galleryid=‘" + id + "‘;"); index = index + 1; if (index % 200 == 0) { Task.Run(() => DoWork(sb.ToString(), dbconnectionStr)).Wait(); sb.Clear(); } //update gallery set gallerytype=right(gallerycontent,3) where galleryid=‘FFA06D63-76E3-C31F-DE4B-EA30DAB78096‘; }); if (sb != null && sb.Length > 0) { DoWork(sb.ToString(), dbconnectionStr); } } msg = "ok"; } catch (Exception ex) { msg = ex.Message; } } private void DoWork(string sql, string connectionDbStr) { using (MySqlConnection conn = new MySqlConnection(connectionDbStr)) { conn.Open(); using (MySqlCommand comm = new MySqlCommand()) { comm.CommandText = sql; comm.CommandType = System.Data.CommandType.Text; comm.Connection = conn; comm.ExecuteNonQuery(); } } } } }

 

.NetCore3.1读取一个文件夹下面的所有文件资源并重新命名以及大批量更新及写入数据

标签:exists   clear   高效   message   database   HERE   environ   core   context   

原文地址:https://www.cnblogs.com/Fengge518/p/12248303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!