获取本地文件然后批量存储到数据库

能遍历路径获取全部数据(根据File.length)获取当前遍历的进度条,然后存储到表里面,并通过SqlBulkCopy的WriteToServer方法存储到数据库中,有进度条。

先建立一个目录结构体 用来存储数据

默认结构是

public struct FileTree
{
public string FileName;
public long FileSize;
/// <summary>
/// 0是文件夹。1是文件
/// </summary>
public int Type;
//如果想修改上级目录名称等可以不写路径,根据HeighID遍历路径
public string path;
//上级id
public string HeighID;
//完整ID 默认 E盘_1_1 是当前文件夹id(int 自增)+HeighID
public string CompleteID;
//层次结构
public List<FileTree> FileOrDirData;
}

因为批量插入走的路线是SqlBulkCopy.WriteToServer ,先建立一个表,表头名称和字段类型要和数据库的一致。

public long Size;//通过size判断遍历进度
Expression<Func<FileTree, FileTree>> AllFileTree = a => new FileTree { };
public static DataTable dt1 = new DataTable();

dt1.Columns.Add("FileName", typeof(string));
                dt1.Columns.Add("FileSize", typeof(long));
                dt1.Columns.Add("type", typeof(int));
                dt1.Columns.Add("path", typeof(string));
                // dt1.Columns.Add("FileID", typeof(int));
                dt1.Columns.Add("HeighID", typeof(string));
                dt1.Columns.Add("CompleteID", typeof(string));

然后递归遍历数据库

   FileTree FileTree1 = t.GetExpression(new FileTree { FileName = "E盘", CompleteID = "01E", HeighID = "0", path = "", Type = -1 }, "E:\\", out size);
   public FileTree GetExpression(FileTree filetree, string Path, out long driver)
        {
            driver = 0;
            if (!Directory.Exists(Path))
            return filetree;
            DirectorySecurity dir = new DirectorySecurity(Path, AccessControlSections.Access);
            if (!dir.AreAccessRulesProtected && !dir.AreAuditRulesProtected)
            {
                long DirSize = 0;
                int i = 0;
                filetree.FileOrDirData = new List<FileTree>();
                foreach (DirectoryInfo item in new DirectoryInfo(Path).GetDirectories())
                {
                    if (item.FullName.Contains("System Volume Information"))
                        continue;
                    i++;
                    DirSize = 0;
                    /*filetree.FileOrDirData.Add(*/
                    GetExpression(new FileTree { FileName = item.Name, /*FileID = i,*/ path = item.FullName, HeighID = filetree.CompleteID, CompleteID = filetree.CompleteID + "_" + i, Type = 0 }, item.FullName, out DirSize);
                    //filetree.FileSize += DirSize;
                    driver += DirSize;
                    dt1.Rows.Add(item.Name, DirSize, 0, item.FullName,/* i,*/ filetree.CompleteID, filetree.CompleteID + "_" + i);
                }
                foreach (FileInfo item in new DirectoryInfo(Path).GetFiles())
                {
                    i++;
                    driver += item.Length;
                    Size += item.Length;
                    //filetree.FileOrDirData.Add(new FileTree { FileName = item.Name, FileID = i, path = item.FullName, HeighID = filetree.FileID, CompleteID = filetree.CompleteID + "_" + i, FileSize = item.Length, Type = 1 });
                    dt1.Rows.Add(item.Name, item.Length, 1, item.FullName/*, i*/, filetree.CompleteID, filetree.CompleteID + "_" + i);
                    // filetree.FileSize += item.Length;
                }
            }
            // filetree.FileOrDirData.Add(GetExpression(filetree, Path));
            //}
            return filetree;
        }
    }

然后对表dt1进行批量插入操作.

using (SqlConnection conn = new SqlConnection("Server = .; Database =DB; user = sa; pwd =*********"))
            {
                SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
                //数据库的名称。
                bulkCopy.DestinationTableName = "AllFileData";
                //一次写入数据库的数量,因为如果数目过大会造成崩溃等,只单纯的定义数量,但不同的数量速度有影响。
                bulkCopy.BatchSize = count ?? ExpressionWithFile.dt1.Rows.Count;
                //进度条使用,当插入多少条触发BulkCopy_SqlRowsCopied事件。
                bulkCopy.NotifyAfter = ExpressionWithFile.dt1.Rows.Count / 100;
                bulkCopy.SqlRowsCopied += BulkCopy_SqlRowsCopied;
                conn.Open();
                if (ExpressionWithFile.dt1 != null && ExpressionWithFile.dt1.Rows.Count != 0)
                {
                    bulkCopy.WriteToServer(ExpressionWithFile.dt1);
                }
                bulkCopy.Close();
                new Action(() => { progressBarX1.Value = progressBarX1.Maximum; progressBarX1.Text = "目录提取完成"; }).Invoke();
            }

             private void BulkCopy_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
              {
             ++progressBarX1.Value;
               }

 

 

posted @ 2018-02-26 17:52  国产小品牌  阅读(751)  评论(0编辑  收藏  举报