Add attributes of multiple files to the database

Source: Internet
Author: User

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19312012Y-0.gif "alt =" y_0017.gif "/> is still a form of communication between forms and database bridges. Previously, we used to add a record at one time. This time, we needed to add all the records at one time. It was really difficult to get the record. We knew it was a pitfall and would jump in again, no way. Later, after constantly checking the information, I learned how to write a rough picture. This process requires four modules:

(1) entity class. Including the attribute fields of the file, that is, the column names of the corresponding tables in the database, and the attributes of these fields. This class is mainly used to strip the relationship between applications and databases. With this entity class, you can perform their own operations without affecting each other.

(2) DBHlper class. This is a common database connector class, a function class, a database operation class, which usually contains the method for connecting to the database. In the project process, as long as we have this class and call the methods in this class, we can achieve database access. However, it does not include the specific data operation method. It just sets up a bridge between programs and databases. This DBHelper class is only responsible for throwing specific SQL statements into the database.

(3) data category. This is a data entity class of a specific entity class that specifically specifies a class for specific operations on the database. It mainly publishes SQL statements and throws SQL statements to the DBHelper class, then, the 'help' class is thrown into the database to perform a series of operations on the database, such as insertion, deletion, and update.

(4) Presentation Layer class. It is used to display data to users, that is, the Windows interface. This is relatively easy. It is not easy to use the above classes.

The following describes how to insert the attributes of all sub-files in a directory and the sub-directories of the Sun Tzu files to a specific table in the database at a time. Look at the Code:

Entity classes are easy to skip. Let's look at the other two special classes:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19312023Y-1.gif "alt =" j_0057.gif "/> generic data category:

Public class DBhelper {// connection string. Public string connString = @ "server = (local); database = AllFilesDB; uid = sa; pwd = 514420 ;"; /// <summary> /// obtain a data table by means of automatic opening // </summary> /// <param name = "SQL"> </param>/ // <param name = "ps"> </param> // <returns> DataTable dt </returns> public DataTable getTable (string SQL, sqlParameter [] ps) {SqlConnection connection = new SqlConnection (connString); SqlCommand command = connection. createCommand (); command. commandText = SQL; command. parameters. addRange (ps); // After the database is connected and the parameters are passed in, use the able class to fill the data table DataTable dt = new DataTable (); SqlDataAdapter sda = new SqlDataAdapter (command ); sda. fill (dt); return dt ;}}

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19312023Y-1.gif "alt =" j_0057.gif "/> data category:

Public class FileService {// Add public void AddFileInfos (CardFile cf) to the database {// Insert operations to the database) string SQL = "Insert into AllFiles (fileName, fileDirectory, IsDirectory) values (@ fileName, @ fileDirectory, @ IsDirectory) "; SqlParameter [] ps = new SqlParameter [] {new SqlParameter (" @ fileName ", cf. fileName), new SqlParameter ("@ fileDirectory", cf. fileDirectory), new SqlParameter ("@ IsDirectory", cf. isDirectory1),}; // methods in DBhelper are not declared as class methods, but object methods. Therefore, the method DBhelper db = new DBhelper () can be called only when Western medicine creates an object (); db. getTable (SQL, ps );}}

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19312023Y-1.gif "alt =" j_0057.gif "/> presentation layer class:

Public partial class Form1: Form {public Form1 () {// initialize InitializeComponent ();} private void btnBrowers_Click (object sender, EventArgs e) {// dialog box prompting users to select the folder path FolderBrowserDialog obd = new FolderBrowserDialog (); // After the dialog box is displayed, two confirmation and cancellation are displayed.) DialogResult dr = obd. showDialog (); // if the returned result is canceled, it means that the user clicks the cancel button if (dr = DialogResult. cancel) {return;} // you can click the OK button to select the path string path = obd. selectedPath; // enter the selected token in the authorization box this.txt FilePath. text = path ;} /// <summary> /// display a function of the file attribute /// </summary> /// <param name = "path"> </param> private static void ShowDirectoryInfo (string path) {FileService fs = new FileService (); DirectoryInfo di = new DirectoryInfo (path); // multiple subdirectories in the root directory form a set.) DirectoryInfo [] dis = di. getDirectories (); // get all the sub-files in the root directory. Multiple Sub-files form a set.) FileInfo [] FD = di. getFiles (); foreach (DirectoryInfo temp in dis) {// Add the Directory attribute value as a CardFile object to the database CardFile cf = new CardFile (); // assign the directory name to the object name attribute cf. fileName = temp. name; cf. fileDirectory = temp. parent. name; cf. isDirectory1 = true; ShowDirectoryInfo (temp. fullName); fs. addFileInfos (cf);} foreach (FileInfo temp in FD) {// Add the object attribute value to the database as a CardFile object. CardFile cf = new CardFile (); // assign the file name to the object name attribute cf. fileName = temp. name; cf. fileDirectory = temp. directory. name; cf. isDirectory1 = false; fs. addFileInfos (cf) ;}// set the 'add' Button Function private void btnAdd_Click (object sender, EventArgs e) {string path = this.txt FilePath. text; ShowDirectoryInfo (path );}}}

The last step is to make a simple window that meets the requirements. This is easier.

650) this. width = 650; "title =" Capture. PNG "src =" http://www.bkjia.com/uploads/allimg/131228/193120AS-4.png "alt =" 211059816.png"/>

I am growing up with continuous efforts and progress ..

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1931202E1-5.gif "alt =" j_0047.gif "/> Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1931202E1-5.gif "alt =" j_0047.gif "/>

This article is from the "Ajax girl" blog!

Related Article

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.