.NetCore3.1获取文件并重新命名以及大批量更新及写入数据

摘要:
usingSystem.Threading.Tasks;=空&uid=风格;database=BigDataTest;intindex=0;for(inti=0;dbconnectionStr))Wait();=空&ViewBag.totalTime=watch.Elapsed.TotalSeconds;
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获取文件并重新命名以及大批量更新及写入数据》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇centos安装redis并开启多个redis实例Function程序设计及应用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

TinyMCE实现word图片自动转存

由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题 考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来解决这一问题 发现不管什么编辑器只要将图片转换成base64后...

SharePoint Server 2013开发之旅(三):为SharePoint Server配置App开发、部署、管理环境

上一篇我讲解了如何利用微软提供的Office 365开发人员网站,进行在线的SharePoint App开发,这当然是不错的一个选择,因为你可以快速地进行开发和调试。(仅仅针对App开发而言)。但是,你可能会想到一个问题,假设我们在企业中已经有了一组SharePoint Server服务器,那么如何让他们也能用来进行App开发、部署并且最终用来统一管理所有...

vue-i18n web 前端国际化

vue-i18n是一个针对于vue的国际化插件,使用非常简单 1. 下载包 npm install vue-i18n  2、创建中、英文包文件 创建两个文件,一个为zh.js代表中文,en.js代表英文,具体内容格式如下 zh.js文件                     en.js文件     3、配置main.js // 引入插件和语言包 imp...

在vue项目中使用monaco-editor

monaco-editor: https://github.com/Microsoft/monaco-editor 在ESM中的使用官方也有对应文档:https://github.com/Microsoft/monaco-editor/blob/master/docs/integrate-esm.md 现基于vue-cli2的项目做具体步骤说明: 1. 安...

filter()数组遍历

filter() 方法对源数组的每个元素判断。返回符合要求的元素,并将他们组成一个新的数组 特性: 1、filter()不会改变源数组 2、filter()返回新数组 语法: arr.filter(callback(currentValue [, index [, array]])[, thisArg]) 参数: arr.filter()有三个参数,...

WordPress标题函数wp_title()详解

在wp_title()中通常是在页面头部的title元素中。当wp_title()在主页主循环(loop)外时,可以用在模板的任何地方。 用法: 1 <?php wp_title( $sep, $echo, $seplocation ); ?> 参数: $sep (字符串)(可选)显示在文章标题前后的文字信息(如分隔符)。 默认情况下(若分隔...