文件夹文件遍历并插入数据库的操作,IO Directory File的递归操作
在我们管理内容管理系统时,数据量大时,对机器的依赖性就比较强了,比如,我要将一个文件夹中的很多图片上传到网站,一个个上传会很花时间,就想到了通过遍历文件夹得到文件名,并将路径与文件保存到数据库中对应的字段。
下面写两个实例,
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 打开连接
/// </summary> static void Main(string[] args)
{
string dirp = @"C:\Users\sihang\Desktop\博鳌照片";
DirectoryInfo mydir = new DirectoryInfo(dirp);
StringBuilder sb = new StringBuilder();
foreach (FileInfo fi in mydir.GetFiles())
{
string thumbimg = "/uploadfiles/article/150403/" + fi.Name;
string title = (fi.Name).Substring(, fi.Name.LastIndexOf("."));
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string web = "新华网";
string infoimg = "/uploadfiles/article/150403/" + fi.Name;
//Console.WriteLine(thumbimg);
//Console.WriteLine(title);
//Console.WriteLine(date);
//Console.WriteLine(web);
//Console.WriteLine(infoimg);
//Console.WriteLine("/uploadfiles/article/150403/" + fi.Name);
//Console.WriteLine((fi.Name).Substring(0, fi.Name.LastIndexOf(".")));
//Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//Console.WriteLine("新华网");
//sd += "insert into cc ;";
sb.Append("insert into jcms_normal_content (Img,Title,AddDate,CustomField05,CustomField04,ChannelId,ClassId,IsPass,IsImg,ReadGroup,Module,TemplateId) values ('" + thumbimg + "','" + title + "','" + date + "','" + web + "','" + infoimg + "','2','12','1','1','-1','Picture','1011');\r\n");
} Console.WriteLine(sb.ToString());
//Console.ReadLine();
}
}
}
这是将桌面上博鳌照片的文件夹里的所有图片路径及图片名称作为标题保存到对应的字段,并将sql脚本输出到cmd窗口中去了
示例2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ string dirv = @"D:\video\ysxs";
DirectoryInfo mydir = new DirectoryInfo(dirv);
StringBuilder sb = new StringBuilder();
foreach (FileInfo fi in mydir.GetFiles())
{
string title = (fi.Name).Substring(, fi.Name.LastIndexOf("."));
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//写法1
string filepath = @"D:\video\ysxs" + fi.Name;
sb.Append("insert into jcms_normal_content (Title,AddDate,ClassId,Editor,IsPass,ReadGroup,Module,TemplateId,CustomField01) values ('" + title + "','" + date + "','8','admin','1','-1','news','11','" + filepath + "');\r\n");
textBox1.Text = sb.ToString(); //写法2
//string s = @"insert into jcms_normal_content ((Title,AddDate,ClassId,Editor,IsPass,ReadGroup,Module,TemplateId,CustomField01)) values ('{0}','" + date + "','8','admin','1','-1','news','11','D:\\video\\ysxs\\{1}');";
//textBox1.Text += string.Format(s, fi.Name.Replace(".txt", ""), fi.Name) + "\r\n";
} }
}
}
新建winform控制台程序,这样就能将sql输出到textbox1文本框中去了。
文件夹文件遍历并插入数据库的操作,IO Directory File的递归操作的更多相关文章
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- java基础IO删除文件夹文件
/** * 定义一个方法,能够删除任意文件夹,文件夹路径由键盘录入 注意:不要在C盘下做测试,请选定无用的文件夹测试! */ 1.键盘录入 private static File getfile() ...
- JavaSE 文件递归之删除&获取文件夹文件夹中全部的以.jpg的文件的绝对路径
1.递归删除文件 假设一个文件夹以下还有子文件夹,进行删除的话会 报错,这个时候要使用递归的方式来删除这个文件文件夹中的全部文件以及文件夹 package cn.itcast.digui; impor ...
- C#获取文件夹/文件的大小以及占用空间 转摘自:http://www.cnblogs.com/chenpeng-dota/articles/2176470.html
C#获取文件夹/文件的大小以及占用空间 今天,头给了个任务:写个方法,我会给你个路径,计算这个路径所占用的磁盘空间 . 然后,找了很多资料.但大部分都是获取文件夹/文件的大小的.对于占用空间的没有成品 ...
- android多国语言文件夹文件汇总
android多国语言文件夹文件汇总如下: 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values-zh-rHK 英语(美国):values-e ...
- PHP文件夹文件拷贝/复制函数 dir_copy($src = '', $dst = '')
/* * 文件夹文件拷贝 * * @param string $src 来源文件夹 * @param string $dst 目的地文件夹 * @return bool */ function dir ...
- java实现基于关键字的文件夹(文件)的搜索、文件夹(文件)的复制、删除
最近在做一个项目,需要实现这几项功能,上网查了很多资料,自己研究了好几天终于实现了,现在与大家分享一下. 一.JAVA实现文件夹的搜索 在百度搜索N个技术文章,从哪些大牛们共享的资料中终于写出了我 ...
- Shell脚本递归打印指定文件夹中全部文件夹文件
#!/bin/bash #递归打印当前文件夹下的全部文件夹文件. PRINTF() { ls $1 | while read line #一次读取每一行放到line变量中 do [ -d $1/$li ...
- mysql 直接从date 文件夹备份表,还原数据库之后提示 table doesn`t exist的原因和解决方法
补充:正常情况下,建议数据库备份最好用工具进行备份,通过拷贝数据库表进行数据迁移,不同的环境会出现各种不同的意外问题. 背景:今天在整理一个网站的时候,操作系统由于系统自动更新导致一直出现系统蓝屏死机 ...
随机推荐
- C语言的函数
"函数"在英文的翻译是"function",无论在自然科学还是计算机科学都是这个词,而"function"的本意是"功能" ...
- Linux系统升级更新openssh 7.3p1
放在最前面:鉴于网上爬虫猖獗,博客被盗时有发生,这里需要来个链接,大家请认准来自博客园的Scoter:http://www.cnblogs.com/scoter2008,本文将持续更新 最近绿盟给扫描 ...
- 在安装SqlServer2008时,有一项安装程序支持规则,为什么重新启动计算机那一项总是失败
1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager目录,找 ...
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
网上的很多模拟登录程序,大都是通过服务程序apache之类的运行,获取到验证码之后显示在网页上,然后填上再POST出去,这样虽然看起来很友 好,但是既然模拟登录,登录后所干的事情就不一定是短时间完成的 ...
- 匈牙利算法 codevs 2776 寻找代表元
codevs 2776 寻找代表元 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用 ...
- 合工大 OJ 1332 蛇形阵
Description 蛇形针回字阵: 如3*3: 回字阵: 7 6 5 8 1 4 9 2 3 Input 多组数据: 每一行一个正整数n(n为奇数,<26),代表n*n矩阵. Output ...
- java 21-11 数据输入、输出流和内存操作流
IO数据流: 可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in) 数据输出流:DataOutputStream ...
- [转] Linux下防火墙iptables用法规则详及其防火墙配置
from: http://www.cnblogs.com/yi-meng/p/3213925.html 备注: 排版还不错,建议看以上的链接. iptables规则 规则--顾名思义就是规矩和原则,和 ...
- 啊D工具语句 适合Access和Mssql注入
啊D注入工具中使用的SQL注入语句 爆user )) )= | ***** ?Id)) : ?Id : Id 检查SA权限:)))) 爆当前库: )) -- 检查是否为mssql数据库:and exi ...
- Android Studio系列教程一--下载和安装
原文链接:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...