.NetCore3.1獲取文件並重新命名以及大批量更新及寫入數據


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(); } } } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM