Folder file traversal and insertion of database operations, recursive operation of IO Directory files

Source: Internet
Author: User

When we manage the content management system, the data volume is large, the dependence on the machine is relatively strong, for example, I want to upload a folder of many images uploaded to the site, uploading it will take a long time, I think of walking through the folder to get the file name, and the path and file saved to the corresponding field in the database.

The following two examples are written,

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {/// <summary>        ///Open Connection/// </summary>             Static voidMain (string[] args) {            stringDIRP =@"C:\Users\sihang\Desktop\ Boao Photo"; DirectoryInfo Mydir=NewDirectoryInfo (DIRP); StringBuilder SB=NewStringBuilder (); foreach(FileInfo fiinchMydir. GetFiles ()) {stringThumbimg ="/uploadfiles/article/150403/"+fi.                Name; stringtitle = (FI. Name). Substring (0, FI. Name.lastindexof (".")); stringDate = DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss"); stringweb ="Xinhua"; stringInfoimg ="/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 ("Xinhua"); //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 ', ' A ', ' 1 ', ' 1 ', '-1 ', ' picture ', ' 1011 '); \ r \ n"); } Console.WriteLine (sb.)            ToString ()); //console.readline ();        }    }}

This is the desktop on the photo of Boao photos in the folder of all the picture path and picture name as a title to the corresponding field, and the SQL script output to the CMD window.

Example 2:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringDirv =@"D:\video\ysxs"; DirectoryInfo Mydir=NewDirectoryInfo (DIRV); StringBuilder SB=NewStringBuilder (); foreach(FileInfo fiinchMydir. GetFiles ()) {stringtitle = (FI. Name). Substring (0, FI. Name.lastindexof (".")); stringDate = DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss"); //notation 1                stringfilepath =@"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 ', ' one ', '"+ filepath +"'); \ r \ n"); TextBox1.Text=sb.                ToString (); //notation 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 ', ' one ', ' d:\\video\\ysxs\\{1} '); "; //TextBox1.Text + = string. Format (S, fi. Name.replace (". txt", ""), FI. Name) + "\ r \ n";             }        }    }}

Create a new WinForm console program so that you can output SQL to the TextBox1 text box.

Folder file traversal and insertion of database operations, recursive operation of IO Directory files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.