码迷,mamicode.com
首页 > 数据库 > 详细

文件夹文件遍历并插入数据库的操作,IO Directory File的递归操作

时间:2015-07-21 01:13:03      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

在我们管理内容管理系统时,数据量大时,对机器的依赖性就比较强了,比如,我要将一个文件夹中的很多图片上传到网站,一个个上传会很花时间,就想到了通过遍历文件夹得到文件名,并将路径与文件保存到数据库中对应的字段。

下面写两个实例,

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(0, 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(0, 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的递归操作

标签:

原文地址:http://www.cnblogs.com/shy1766IT/p/4662958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!