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获取文件并重新命名以及大批量更新及写入数据的更多相关文章

  1. C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件。

    转自https://blog.csdn.net/liubowei_0312/article/details/53378146 适合将文件写入数据库,远程访问的时候还原1.首先把文件转成十六进制文件流 ...

  2. 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器

    不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...

  3. lr文件下载脚本(文件参数化重命名)

    http://wenku.baidu.com/link?url=6oiIadyF9eFS4VshKbfJDnxrBh2IX919ndi0JO8yoqTRNRNIpavFrZJ9LPVb-FBSfbRY ...

  4. Web 在线文件管理器学习笔记与总结(11)获取文件夹信息 (12)返回上一级操作

    (11)获取文件夹信息 文件夹没有修改操作. index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

  5. 运用CMD命令关于快速获取文件夹名称和快速建立文件夹

    前些天头儿让我建立一本本的文件夹,让后交给我了几个命令,快速获取文件夹的名称和快速建立文件夹,省去了一个个的按F2,一个个的复制,粘贴,一个个的新建,再复制粘贴. 首先讲一下第一个问题,快速获取文件夹 ...

  6. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  7. 从字符串总分离文件路径、命名、扩展名,Substring(),LastIndexOf()的使用;替换某一类字符串,Replace()的用法

    一:从字符串总分离文件路径.命名.扩展名,上图 二:代码 using System; using System.Collections.Generic; using System.ComponentM ...

  8. stat(),lstat(),fstat() 获取文件/目录的相关信息

    stat 的使用 Linux有个命令,ls -l,效果如下: 这个命令能显示文件的类型.操作权限.硬链接数量.属主.所属组.大小.修改时间.文件名.它是怎么获得这些信息的呢,请看下面的讲解. stat ...

  9. [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等

    1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...

随机推荐

  1. Lambda 表达式的基础语法

    1.基础语法 java8引入新的操作符"->"箭头操作符,箭头操作符将Lambda表达式分成两部分 左侧:Lambda 表达式的参数列表,对应抽象方法的参数列表 右侧:需要执 ...

  2. python读取数据写入excel的四种操作

    Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...

  3. windows10激活方法

    原文转自:http://www.ylmfwin100.com/ylmf/8643.html 现在市面上大致有两种主流激活方法,一种是通过激活码来激活,另外一种是通过激活工具来激活.但是激活工具有个弊端 ...

  4. 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)

    问题描述 在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store) 效果类似于本地 ...

  5. SQL Server CPU 利用率毛刺的分析定位与解决

    一.背景 1.1 问题描述 近期发现一台SQL Server的CPU利用率很不稳定,发现不定时的飙升到100%,更可怕的是在业务繁忙时,影响了业务调用,失败率明显增加,所以,减低CPU的利用率,是迫切 ...

  6. 自学linux——7.Linux的自有服务(进阶篇)

    linux自有服务 1.设置主机名 (1)临时设置主机名,需要切换用户(su)使之生效 #hostname主机名 (2)永久设置主机名,需要重启 先找到一个文件[主机名的配置文件]/etc/sysco ...

  7. 洛谷P1377题解

    题面 首先他叫我们建一颗笛卡尔树,所以我们就建一颗笛卡尔树. 然后他说要生成序列中最小的.想到笛卡尔树的一条性质:左<根<右.但是根节点必须先插进去.他的两个儿子用贪心的思想就知道是先选左 ...

  8. vulnhub-靶机Lampiao

    目标信息:攻击机IP地址:192.1681.10 Lampiao靶机IP地址:192.168.1.12 DC-1靶机IP地址:192.168.1.7 目的:获取靶机root权限和靶机设置的所有flag ...

  9. 4 剑指Offer53-在排序数组中查找数字

    统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2  示例 2: 输入: nums = [5,7,7,8,8,10 ...

  10. Linux无法下载GCC

    Linux:ping百度ping不通 因为作业需要今天使用Linux下载GCC编译器,却发生了不可预知的错误: Errors during downloading metadata for repos ...