.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();
}
}
}
}
}
.NetCore3.1获取文件并重新命名以及大批量更新及写入数据的更多相关文章
- C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件。
转自https://blog.csdn.net/liubowei_0312/article/details/53378146 适合将文件写入数据库,远程访问的时候还原1.首先把文件转成十六进制文件流 ...
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...
- lr文件下载脚本(文件参数化重命名)
http://wenku.baidu.com/link?url=6oiIadyF9eFS4VshKbfJDnxrBh2IX919ndi0JO8yoqTRNRNIpavFrZJ9LPVb-FBSfbRY ...
- Web 在线文件管理器学习笔记与总结(11)获取文件夹信息 (12)返回上一级操作
(11)获取文件夹信息 文件夹没有修改操作. index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...
- 运用CMD命令关于快速获取文件夹名称和快速建立文件夹
前些天头儿让我建立一本本的文件夹,让后交给我了几个命令,快速获取文件夹的名称和快速建立文件夹,省去了一个个的按F2,一个个的复制,粘贴,一个个的新建,再复制粘贴. 首先讲一下第一个问题,快速获取文件夹 ...
- 【转】linux C++ 获取文件信息 stat函数详解
stat函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h>定义函数: int stat ...
- 从字符串总分离文件路径、命名、扩展名,Substring(),LastIndexOf()的使用;替换某一类字符串,Replace()的用法
一:从字符串总分离文件路径.命名.扩展名,上图 二:代码 using System; using System.Collections.Generic; using System.ComponentM ...
- stat(),lstat(),fstat() 获取文件/目录的相关信息
stat 的使用 Linux有个命令,ls -l,效果如下: 这个命令能显示文件的类型.操作权限.硬链接数量.属主.所属组.大小.修改时间.文件名.它是怎么获得这些信息的呢,请看下面的讲解. stat ...
- [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等
1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...
随机推荐
- windows系统pycharm终端更改为git bash
引自:https://blog.csdn.net/u011519550/article/details/89855122 设置路径:file--setting--tools--terminal--ap ...
- Flask 之linux部署
1.装python > `[root ~]# yum install gcc [root ~]# wget https://www.python.org/ftp/python/3.6.5/Pyt ...
- 编程思想与算法leetcode_二分算法详解
二分算法通常用于有序序列中查找元素: 有序序列中是否存在满足某条件的元素: 有序序列中第一个满足某条件的元素的位置: 有序序列中最后一个满足某条件的元素的位置. 思路很简单,细节是魔鬼. 二分查找 一 ...
- 基于单机redis的分布式锁实现
最近我们有个服务经常出现存储的数据出现重复,首先上一个系统流程图: 用户通过http请求可以通知任务中心结束掉自己发送的任务,这时候任务中心会通过MQ通知结束服务去结束任务保存数据,由于任务结束数据计 ...
- 谈一下python中的列表
Python标准库基于C语言实现了丰富的序列类型包括元组,列表,字典... 今天我们只谈list(列表) 1 列表(list) 最基础也是最重要的序列类型,他本身可以存放不同数据类型的元素.列表推导是 ...
- 创建函数function
1.创建普通函数 function 函数名称(){ 函数体://封装的代码 } 函数名称()://调用函数 function getSum(){ for(var i=1,sum=0;i<=100 ...
- IIS短文件名漏洞原理与挖掘思路
首先来几个网址先了解一下 https://www.jb51.net/article/166405.htm https://www.freebuf.com/articles/web/172561.htm ...
- 小白学习Vue第五天(v-model实用的地方)
用法一radio单选项 <!-- 添加name男女选项互斥 --> <label for="male"> <input type="radi ...
- DHCP\PXE+kickstart网络装机平台
DHCP概述及原理: DHCP地址分配的四次会话 DISCOVERY -****OFFER -REQUEST -ACK 服务端基本概念: 租期:允许客户机组用IP地址的时间期限,单位为秒 作用 ...
- 数据结构与算法-排序(六)堆排序(Heap Sort)
摘要 堆排序需要用到一种数据结构,大顶堆.大顶堆是一种二叉树结构,本质是父节点的数大于它的左右子节点的数,左右子节点的大小顺序不限制,也就是根节点是最大的值. 这里就是不断的将大顶堆的根节点的元素和尾 ...