コード例 #1
0
ファイル: Database.cs プロジェクト: zeralph/FilesSearch
        private void CalculateMd5ForAllFiles(Action <string> UpdateAction)
        {
            int nbResults = 0;
            int i         = 0;

            using (SqlCeCommand cmd = new SqlCeCommand($@"SELECT COUNT(Id) FROM Files WHERE checksum = '' OR checksum IS NULL", m_connection))
            {
                SqlCeDataReader reader = cmd.ExecuteReader();
                reader.Read();
                nbResults = reader.GetFieldValue <int>(0);
            }
            SqlCeTransaction transaction = m_connection.BeginTransaction();

            using (SqlCeCommand cmd = new SqlCeCommand($@"SELECT Id, name, path, size, checksum FROM Files WHERE checksum = '' OR checksum IS NULL", m_connection))
            {
                SqlCeDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    if (m_bStop)
                    {
                        return;
                    }
                    i++;
                    int    Id = Convert.ToInt32(reader["Id"]);
                    long   s  = Convert.ToInt64(reader["size"]);
                    string p  = Convert.ToString(reader["path"]);
                    string n  = Convert.ToString(reader["name"]);
                    p = Path.Combine(p, n);
                    UpdateAction.Invoke($"Calculate CRC {i}/{nbResults} for {p} ({FileExplorer.GetFormattedSize(s)})");
                    string       fileChecksum = CalculateMD5(p, s);
                    SqlCeCommand cmdUpdate    = new SqlCeCommand($@"UPDATE Files SET checksum = @checksum WHERE Id = @Id", m_connection);
                    cmdUpdate.Parameters.Add("@Id", System.Data.SqlDbType.Int).Value            = Id;
                    cmdUpdate.Parameters.Add("@checksum", System.Data.SqlDbType.NVarChar).Value = fileChecksum;
                    cmdUpdate.ExecuteNonQuery();
                }
                transaction.Commit();
            }
        }