Advertisement
Guest User

Untitled

a guest
Apr 5th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 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.  
  37. var connection = new MySqlConnection("Server=192.168.0.8;Database=magento_stage;Uid=magento;Pwd=MXf2tZka;CharSet=utf8");
  38.  
  39. //Console.WriteLine("Server ip Address :");
  40.  
  41. //var ip = Console.ReadLine().Trim();
  42.  
  43. //if (String.IsNullOrEmpty(ip))
  44. //{
  45. // throw new Exception("Server ip address cannot be NULL");
  46. //}
  47.  
  48. //Console.WriteLine("Database Name : ");
  49.  
  50. //var database = Console.ReadLine().Trim();
  51.  
  52. //if (String.IsNullOrEmpty(database))
  53. //{
  54. // throw new Exception("Data cannot be NULL");
  55. //}
  56.  
  57. //Console.WriteLine("Username : ");
  58.  
  59. //var username = Console.ReadLine().Trim();
  60.  
  61. //if (String.IsNullOrEmpty(username))
  62. //{
  63. // throw new Exception("Username cannot be NULL");
  64. //}
  65.  
  66. //Console.WriteLine("Password : ");
  67.  
  68. //var password = Console.ReadLine().Trim();
  69.  
  70. //if (String.IsNullOrEmpty(password))
  71. //{
  72. // throw new Exception("Password cannot be NULL");
  73. //}
  74.  
  75. Console.WriteLine("Starting Executting ------------- ");
  76.  
  77. //var connection = new MySqlConnection($"Server={ip};Database={database};Uid={username};Pwd={password};CharSet=utf8");
  78. System.IO.StreamWriter file = new System.IO.StreamWriter(@"log.txt", true);
  79.  
  80. try
  81. {
  82. var sql = @"SELECT media.value_id, product.row_id, position, images.value FROM catalog_product_entity as product
  83. join catalog_product_entity_media_gallery_value as media on product.row_id = media.row_id
  84. join catalog_product_entity_media_gallery as images on media.value_id = images.value_id
  85. WHERE product.type_id = 'simple' AND product.sku = '3517200004'
  86. GROUP BY media.value_id, product.row_id, position, images.value ORDER by position";
  87.  
  88. connection.Open();
  89.  
  90. var getAll = new MySqlCommand(sql, connection);
  91.  
  92. var reader = getAll.ExecuteReader();
  93.  
  94. var result = new List<Result>();
  95.  
  96. while (reader.Read())
  97. {
  98. result.Add(new Result()
  99. {
  100. EntityId = reader[1].ToString(),
  101. Value = reader[3].ToString()
  102.  
  103. });
  104. }
  105.  
  106. reader.Close();
  107.  
  108. var updatedEntities = new List<string>();
  109.  
  110. foreach (var r in result)
  111. {
  112. if (!updatedEntities.Contains(r.EntityId))
  113. {
  114. UpdateImageRole(connection, r.EntityId, r.Value);
  115. updatedEntities.Add(r.EntityId);
  116.  
  117. Console.WriteLine($"Data : {DateTime.Now}, Entity Updated {r.EntityId} ");
  118. file.WriteLine($"Data : {DateTime.Now}, Entity Updated {r.EntityId} ");
  119. }
  120. }
  121.  
  122. connection.Close();
  123. file.Close();
  124.  
  125. Console.WriteLine("--------- Done");
  126. Console.ReadKey();
  127. }
  128. catch (Exception ex)
  129. {
  130. Console.WriteLine(ex.Message.ToString());
  131. file.WriteLine(ex.Message.ToString());
  132. file.Close();
  133. connection.Close();
  134. }
  135.  
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement