static void Main(string[] args) { Console.WriteLine("Tool to upload the files from Mc_Resource table to File Service.\r\n"); int totalCount = 0; int successCount = 0; QueriesTableAdapter queryAdapter = null; ResourceTableAdapter resourceAdapter = null; MasterDataSet.ResourceDataTable resourceTable = null; try { queryAdapter = new QueriesTableAdapter(); resourceAdapter = new ResourceTableAdapter(); // Reads configuration. List <string> objectTypesWithPublicAccess = new List <string>(ConfigurationManager.AppSettings["ObjectTypesWithPublicAccess"].Split(',')); string applicationId = Micajah.FileService.Client.Properties.Settings.Default.ApplicationId.ToString(); // Gets top 1000 files from the database. resourceTable = resourceAdapter.GetResources(); int rowsCount = resourceTable.Count; int fileIndex = 0; while (rowsCount > 0) { totalCount += rowsCount; foreach (MasterDataSet.ResourceRow row in resourceTable) { int uploadStatus = 0; // 0 - Failed, 1 - Success, NULL - Not processed. fileIndex++; try { // Gets the file from database. Console.WriteLine("Resource #{0} \"{1}\" (resourceId = {2:N}).", fileIndex, row.Name, row.ResourceId); Console.Write("Getting from database..."); byte[] content = resourceAdapter.GetContent(row.ResourceId); Console.Write(" Done. "); Console.Write("Uploading to File Service..."); Guid objectId = new Guid(row.LocalObjectId); string objectType = null; string name = null; string connectionString = null; switch (row.LocalObjectType) { case "InstanceLogo": objectType = "instance-logo"; MasterDataSet.InstanceRow instanceRow = GetInstance(objectId); if (instanceRow == null) { throw new ApplicationException(string.Format(CultureInfo.InvariantCulture, "Instance with Id = {0} not found.", objectId)); } name = instanceRow.Name; connectionString = GetConnectionString(instanceRow.OrganizationId); break; case "OrganizationLogo": objectType = "organization-logo"; name = queryAdapter.GetOrganiationName(objectId); connectionString = GetConnectionString(objectId); break; } bool publicAccess = objectTypesWithPublicAccess.Contains(objectType); // Uploads to File Service. string checksum = null; string organizationId = objectId.ToString("N"); string departmentId = objectId.ToString("N"); string result = Access.PutFileAsByteArray(applicationId, name, ref organizationId, name, ref departmentId, row.Name, ref content, (!publicAccess), ref checksum , objectId.ToString("N"), objectType, string.Empty, connectionString); if (!Access.StringIsFileUniqueId(result)) { throw new ApplicationException(result); } uploadStatus = 1; successCount++; Console.WriteLine(" Done.\r\n"); } catch (Exception ex) { Console.WriteLine(" Failed.\r\n{0}\r\n", ex.ToString()); } // Updates upload status of the file in the database. resourceAdapter.UpdateUploadStatus(row.ResourceId, uploadStatus); } // Gets next 1000 files from the database. resourceTable = resourceAdapter.GetResources(); rowsCount = resourceTable.Count; } Console.WriteLine(@" Total files found: {0} Uploaded: {1} Failed: {2}" , totalCount, successCount, totalCount - successCount); } catch (Exception ex) { Console.WriteLine("\r\n{0}\r\n", ex.ToString()); } finally { if (queryAdapter != null) { queryAdapter.Dispose(); } if (resourceAdapter != null) { resourceAdapter.Dispose(); } if (resourceTable != null) { resourceTable.Dispose(); } } Console.WriteLine("\r\nPress any key to quit."); Console.ReadKey(); }