Replacement for JDIS Importer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

191 lignes
10 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. namespace jdis_import
  8. {
  9. public class CSVObject
  10. {
  11. public string CustomerNumber { get; set; }
  12. public string CustomerName { get; set; }
  13. public string Address { get; set; }
  14. public string Address2 { get; set; }
  15. public string City { get; set; }
  16. public string State { get; set; }
  17. public string ZipCode { get; set; }
  18. public string EquipmentNumber { get; set; }
  19. public string SerialNumber { get; set; }
  20. public string Model { get; set; }
  21. public string MeterReading { get; set; }
  22. public string Year { get; set; }
  23. public string FamilyCode { get; set; }
  24. public string FamilyDescription { get; set; }
  25. public string Make { get; set; }
  26. public string LastServiceDate { get; set; }
  27. public string DBSWorkOrderNumber { get; set; }
  28. public string DateActualStart { get; set; }
  29. public string CodeAndDescriptionOriginal { get; set; }
  30. public string CodeAndDescription { get; set; }
  31. public string Store { get; set; }
  32. public string StoreDescription { get; set; }
  33. public string ContractTypeCode { get; set; }
  34. public string JobSiteContactName { get; set; }
  35. public string JobSiteContactPhone { get; set; }
  36. public string PriorityCode { get; set; }
  37. public string SpecialInstructions { get; set; }
  38. public string PartNumber { get; set; }
  39. public string PartNumberDescription { get; set; }
  40. public string ReferenceDocNumber { get; set; }
  41. public string Quantity { get; set; }
  42. public string SegmentNumber { get; set; }
  43. public string ServiceTech { get; set; }
  44. public string TechName { get; set; }
  45. public string InspectionType { get; set; }
  46. public string ScheduledStartDate { get; set; }
  47. public string SQLStatement { get; set; }
  48. public static int MachineExists(string serialnumber, string connStr)
  49. {
  50. int? id = 0;
  51. string sql = string.Format("Select ID from equipment where serialnumber = '{0}'", serialnumber);
  52. using (SqlConnection conn = new SqlConnection(connStr))
  53. {
  54. conn.Open();
  55. SqlCommand cmd = conn.CreateCommand();
  56. cmd.CommandText = sql;
  57. cmd.CommandType = CommandType.Text;
  58. id = Convert.ToInt32(cmd.ExecuteScalar());
  59. if (id > 0)
  60. {
  61. cmd.CommandText = string.Format("Select ModelID from Equipment where ID = {0}", id);
  62. id = Convert.ToInt32(cmd.ExecuteScalar());
  63. }
  64. }
  65. return Convert.ToInt32(id);
  66. }
  67. public void Validate()
  68. {
  69. if (this.CustomerNumber.Length > 50) throw new ApplicationException(string.Format("Customer Number too long. Text: {0}", this.CustomerNumber));
  70. if (this.CustomerName.Length > 50) throw new ApplicationException("Customer Name too long.");
  71. if (this.Address.Length > 50) throw new ApplicationException("Address too long.");
  72. if (this.Address2.Length > 50) throw new ApplicationException("Address2 too long.");
  73. if (this.City.Length > 50) throw new ApplicationException("City too long.");
  74. if (this.State.Length > 3) throw new ApplicationException("State too long.");
  75. if (this.ZipCode.Length > 50) throw new ApplicationException("ZipCode too long.");
  76. if (this.EquipmentNumber.Length > 50) throw new ApplicationException("Equipment Number too long.");
  77. if (this.SerialNumber.Length > 50) throw new ApplicationException("Serial Number too long.");
  78. if (this.Model.Length > 50) throw new ApplicationException("Model too long.");
  79. if (this.MeterReading.Length > 50) throw new ApplicationException("Meter Reading too long.");
  80. if (this.Year.Length > 50) throw new ApplicationException("Year too long.");
  81. if (this.FamilyCode.Length > 50) throw new ApplicationException("Family Code too long.");
  82. if (this.FamilyDescription.Length > 50) throw new ApplicationException("Family Description too long.");
  83. if (this.Make.Length > 50) throw new ApplicationException("Make too long.");
  84. if (this.LastServiceDate.Length > 50) throw new ApplicationException("Last Service Date too long.");
  85. if (this.DBSWorkOrderNumber.Length > 50) throw new ApplicationException("DBS Work Order Number too long.");
  86. if (this.DateActualStart.Length > 50) throw new ApplicationException("Date Actual Start too long.");
  87. if (this.CodeAndDescriptionOriginal.Length > 50) throw new ApplicationException("Code and Description Original too long.");
  88. if (this.Store.Length > 50) throw new ApplicationException("Store too long.");
  89. if (this.StoreDescription.Length > 50) throw new ApplicationException("Store Description too long.");
  90. if (this.ContractTypeCode.Length > 50) throw new ApplicationException("Contact Type Code too long.");
  91. if (this.JobSiteContactName.Length > 50) throw new ApplicationException("Job Site Contact Name too long.");
  92. if (this.JobSiteContactPhone.Length > 50) throw new ApplicationException("job Site contact Phone too long.");
  93. if (this.PriorityCode.Length > 50) throw new ApplicationException("Priority Code too long.");
  94. if (this.PartNumber.Length > 50) throw new ApplicationException("Part Number too long.");
  95. if (this.PartNumberDescription.Length > 50) throw new ApplicationException("Part Number Description too long.");
  96. if (this.ReferenceDocNumber.Length > 50) throw new ApplicationException("Reference Document Number too long.");
  97. if (this.Quantity.Length > 50) throw new ApplicationException("Quantity too long.");
  98. if (this.SegmentNumber.Length > 50) throw new ApplicationException("Segment too long.");
  99. if (this.ServiceTech.Length > 50) throw new ApplicationException("Service Tech too long.");
  100. if (this.TechName.Length > 50) throw new ApplicationException("Tech Name too long.");
  101. if (this.InspectionType.Length > 50) throw new ApplicationException("Inspection Type too long.");
  102. }
  103. public static string WriteToDB(List<CSVObject> items, SqlConnection conn)
  104. {
  105. SqlCommand cmd = conn.CreateCommand();
  106. cmd.Connection = conn;
  107. cmd.CommandType = CommandType.Text;
  108. string insertcmd = @"Insert Into Results_Brandt(CustomerNumber, CustomerName, [Address], Address2, City, [State], ZipCode, EquipmentNumber, SerialNumber, [Model], MeterReading, [Year], FamilyCode, FamilyDescription, Make, LastServiceDate, DBSWorkOrderNumber, DateActualStart, CodeAndDescriptionOriginal, CodeAndDescription, Store, StoreDescription, ContractTypeCode, JobSiteContactName, JobSiteContactPhone, PriorityCode, SpecialInstructions, PartNumber, PartNumberDescription, ReferenceDocNumber, Quantity, SegmentNumber, ServiceTech, TechName, InspectionType) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}', '{34}')";
  109. List<string> msg = new List<string>();
  110. string msgs = string.Empty;
  111. foreach (var item in items)
  112. {
  113. cmd.CommandText = string.Format(insertcmd,
  114. item.CustomerNumber,
  115. item.CustomerName,
  116. item.Address,
  117. item.Address2,
  118. item.City,
  119. item.State,
  120. item.ZipCode,
  121. item.EquipmentNumber,
  122. item.SerialNumber,
  123. item.Model,
  124. item.MeterReading,
  125. item.Year,
  126. item.FamilyCode,
  127. item.FamilyDescription,
  128. item.Make,
  129. item.LastServiceDate,
  130. item.DBSWorkOrderNumber,
  131. item.ScheduledStartDate,
  132. item.CodeAndDescriptionOriginal,
  133. item.CodeAndDescription,
  134. item.Store,
  135. item.StoreDescription,
  136. item.ContractTypeCode,
  137. item.JobSiteContactName,
  138. item.JobSiteContactPhone,
  139. item.PriorityCode,
  140. string.IsNullOrEmpty(item.SpecialInstructions) ? string.Empty : item.SpecialInstructions,
  141. item.PartNumber,
  142. item.PartNumberDescription,
  143. item.ReferenceDocNumber,
  144. item.Quantity,
  145. item.SegmentNumber,
  146. item.ServiceTech,
  147. item.TechName,
  148. item.InspectionType);
  149. try
  150. {
  151. cmd.ExecuteNonQuery();
  152. cmd.Dispose();
  153. }
  154. catch (Exception ex)
  155. {
  156. item.SQLStatement = cmd.CommandText;
  157. msg.Add(ex.Message);
  158. }
  159. }
  160. if (msg.Count > 0) return string.Join("\r\n", msg);
  161. else return string.Empty;
  162. }
  163. public static void WriteCSV<T>(IEnumerable<T> items, string path)
  164. {
  165. if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
  166. Type itemType = typeof(T);
  167. var props = itemType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  168. using (var writer = new System.IO.StreamWriter(path))
  169. {
  170. string[] clmns = props.Select(p => p.Name).ToArray();
  171. string header = string.Join(",", clmns);
  172. writer.WriteLine(header);
  173. foreach (var item in items)
  174. {
  175. List<string> vals = new List<string>();
  176. foreach (string prop in clmns)
  177. vals.Add(item.GetType().GetProperty(prop).GetValue(item, null).ToString());
  178. string valueline = string.Join(",", vals);
  179. writer.WriteLine(valueline);
  180. }
  181. }
  182. }
  183. }
  184. }