Batch Transfer of massive files in the database

Source: Internet
Author: User

The process is like this! Add A record to Table A of the database. ** in the system, there will be 5 N more files under directory B. As the system has been running for more than three years, the number of files in directory B has reached more than 2 MB, and these files are frequently accessed by users. As a result, the problem arises: on the one hand, the speed of file access by users is slow; on the other hand, there are too many files and it is difficult to maintain them.

What should we do? After A long time, we found that there is A time field in Table A that won't be changed. If you intercept the year + month of the input time to create the sub-directory name under the B directory and archive the new files in the current month under the sub-directory, isn't that all right? After the newly added files are processed, you can pay a weekly discount on the archiving of old files because the files have to be migrated to the new subdirectories.

The following is the main code for file migration:

 
 
  1. Static void Main (string [] args)
  2. {
  3. String paperPath = ConfigurationManager. receivettings ["PaperBuildPath"];
  4. Console. WriteLine (string. Format ("Exam Directory: {0}", paperPath ));
  5. Console. WriteLine ();
  6. Console. WriteLine ("is the directory correct? Press any key ......");
  7. Console. WriteLine ();
  8. Console. ReadKey ();
  9. String [] files = Directory. GetFiles (paperPath );
  10. Int num = 0;
  11. PublicExam [] list = Gateway. Default. FindArray <PublicExam> ();
  12. Foreach (PublicExam publicExam in list)
  13. {
  14. Foreach (string file in files)
  15. {
  16. // Source file name (after path removal)
  17. String fileName = file. Split ('\'). Last ();
  18. If (fileName. StartsWith (publicExam. FGuid. ToString (), StringComparison. CurrentCultureIgnoreCase ))
  19. {
  20. // Target folder
  21. String destFilePath = paperPath + publicExam. FInputTime. ToString ("yyyyMM ");
  22. If (Directory. Exists (destFilePath) = false)
  23. Directory. CreateDirectory (destFilePath );
  24. // Target file name
  25. String destFileName = destFilePath + "\" + fileName;
  26. If (File. Exists (destFileName ))
  27. File. Delete (destFileName );
  28. Console. WriteLine (string. Format ("migrating file: {0}", fileName ));
  29. // Migrate the file
  30. File. Move (file, destFileName );
  31. Num ++;
  32. }
  33. }
  34. }
  35. Console. WriteLine ();
  36. Console. WriteLine (string. Format ("A total of {0} files are migrated. Press any key to exit...", num ));
  37. Console. ReadKey ();
  38. }

The above example references how to use the File Class and Directory Class in MSDN.

Run the following command:

Tips:

Directory Name (Year + month) for example: 201101

C # => DateTime. Now. ToString ("yyyyMM ")

SQL => convert (varchar (6), getdate (), 112)

Of course, file migration alone is not enough. There is still a lot of work to be done, such as modifying programs and updating database table records. I know that this "operation" does not comply with the open-close principle.

Measure the test taker's knowledge about the Oracle file system mechanism.

Explain how MongoDB accelerates the storage of physical files and SQUID

Description of the DB2 external file format

Analysis of DB2 external file format

Oracle Database File Management Experience

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.