Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace UpdateImageRoles
  7. {
  8. class Program
  9. {
  10. public class Result {
  11. public string EntityId { get; set; }
  12. public string Value { get; set; }
  13. }
  14.  
  15. public static bool UpdateImageRole(MySqlConnection connection, string enittyId, string value)
  16. {
  17. try
  18. {
  19. var sql = $@"UPDATE catalog_product_entity_varchar
  20. SET value = '{value}'
  21. WHERE row_id = '{enittyId}' AND attribute_id in (174,176,178);";
  22.  
  23. var command = new MySqlCommand(sql, connection);
  24. var r = command.ExecuteNonQuery();
  25.  
  26. return true;
  27. }
  28. catch(Exception e) {
  29. return false;
  30. }
  31. }
  32.  
  33. static void Main(string[] args)
  34. {
  35. //var connection = new MySqlConnection("Server=192.168.65.215;Database=magento;Uid=magento;Pwd=magento;CharSet=utf8");
  36. Console.WriteLine("Server ip Address :");
  37.  
  38. var ip = Console.ReadLine().Trim();
  39.  
  40. if (String.IsNullOrEmpty(ip))
  41. {
  42. throw new Exception("Server ip address cannot be NULL");
  43. }
  44.  
  45. Console.WriteLine("Database Name : ");
  46.  
  47. var database = Console.ReadLine().Trim();
  48.  
  49. if (String.IsNullOrEmpty(database))
  50. {
  51. throw new Exception("Data cannot be NULL");
  52. }
  53.  
  54. Console.WriteLine("Username : ");
  55.  
  56. var username = Console.ReadLine().Trim();
  57.  
  58. if (String.IsNullOrEmpty(username))
  59. {
  60. throw new Exception("Username cannot be NULL");
  61. }
  62.  
  63. Console.WriteLine("Password : ");
  64.  
  65. var password = Console.ReadLine().Trim();
  66.  
  67. if (String.IsNullOrEmpty(password))
  68. {
  69. throw new Exception("Password cannot be NULL");
  70. }
  71.  
  72. Console.WriteLine("Starting Executting ------------- ");
  73.  
  74. var connection = new MySqlConnection($"Server={ip};Database={database};Uid={username};Pwd={password};CharSet=utf8");
  75. System.IO.StreamWriter file = new System.IO.StreamWriter(@"log.txt", true);
  76.  
  77. try
  78. {
  79. var sql = @"SELECT media.value_id, product.row_id, position, images.value FROM catalog_product_entity as product
  80. join catalog_product_entity_media_gallery_value as media on product.row_id = media.row_id
  81. join catalog_product_entity_media_gallery as images on media.value_id = images.value_id
  82. WHERE product.type_id = 'simple'
  83. GROUP BY media.value_id, product.row_id, position, images.value ORDER by position";
  84.  
  85. connection.Open();
  86.  
  87. var getAll = new MySqlCommand(sql, connection);
  88.  
  89. var reader = getAll.ExecuteReader();
  90.  
  91. var result = new List<Result>();
  92.  
  93. while (reader.Read())
  94. {
  95. result.Add(new Result()
  96. {
  97. EntityId = reader[1].ToString(),
  98. Value = reader[3].ToString()
  99.  
  100. });
  101. }
  102.  
  103. reader.Close();
  104.  
  105. var updatedEntities = new List<string>();
  106.  
  107. foreach (var r in result)
  108. {
  109. if (!updatedEntities.Contains(r.EntityId))
  110. {
  111. UpdateImageRole(connection, r.EntityId, r.Value);
  112. updatedEntities.Add(r.EntityId);
  113.  
  114. Console.WriteLine($"Data : {DateTime.Now}, Entity Updated {r.EntityId} ");
  115. file.WriteLine($"Data : {DateTime.Now}, Entity Updated {r.EntityId} ");
  116. }
  117. }
  118.  
  119. connection.Close();
  120. file.Close();
  121.  
  122. Console.WriteLine("--------- Done");
  123. Console.ReadKey();
  124. }
  125. catch (Exception ex)
  126. {
  127. Console.WriteLine(ex.Message.ToString());
  128. file.WriteLine(ex.Message.ToString());
  129. file.Close();
  130. connection.Close();
  131. }
  132.  
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement