Facebook
From Bistre Meerkat, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 252
  1. /* Microsoft SQL Server Integration Services Script Component
  2. *  Write scripts using Microsoft Visual C# 2008.
  3. *  ScriptMain is the entry point class of the script.*/
  4.  
  5.  
  6. using System;
  7. using System.Data;
  8. using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
  9. using Microsoft.SqlServer.Dts.Runtime.Wrapper;
  10. using System.Net;
  11. using System.IO;
  12. using System.Globalization;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Windows.Forms;
  16. using System.Collections.Generic;
  17. using System.Runtime.Serialization;
  18. using System.Web.Extensions;
  19. using System.Web.Script.Serialization;
  20. using Newtonsoft.Json;
  21.  
  22. [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
  23. public class ScriptMain : UserComponent
  24. {
  25.    
  26.     public static string responseFromServer = "";
  27.     // public static string URL = "http://172.16.5.99:55561/GIS/ProductSvc/REST/GetProductList/test/1/*/*";
  28.     public static string URL = "http://172.16.5.233:55561/GIS/ProductSvc/REST/GetProductList/skarp/1/*/TVK!|^U;ANR<=39";
  29.     public static int temp = 0;
  30.  
  31.    // public static string test2 = "";
  32.     //public static string test3 = "";
  33.    // public static string test1 = "";
  34.  
  35.     public override void PreExecute()
  36.     {
  37.         base.PreExecute();
  38.         /*
  39.           Add your code here for preprocessing or remove if not needed
  40.         */
  41.     }
  42.  
  43.     public override void PostExecute()
  44.     {
  45.         base.PostExecute();
  46.         /*
  47.           Add your code here for postprocessing or remove if not needed
  48.           You can set read/write variables here, for example:
  49.           Variables.MyIntVar = 100
  50.         */
  51.     }
  52.  
  53.  
  54.    
  55.  
  56.     [DataContract]
  57.     public class Article
  58.     {
  59.         [DataMember]
  60.         public List<ProductText> ProductText{ get; set; }
  61.         public string ProductNo {get; set;}
  62.         public string Description { get; set; }
  63.         public string Category { get; set; }
  64.         public int AmountPerUnit { get; set; }
  65.         public decimal Price { get; set; }
  66.         public string DiscountSalesCode { get; set; }
  67.         public string SupplierNo { get; set; }
  68.         public string SupplierProductNo { get; set; }
  69.         public decimal PurchasePrice { get; set; }
  70.         public string PurchaseCurrencyCode { get; set; }
  71.         public decimal StandardPrice { get; set; }
  72.         public string OriginType { get; set; }
  73.     }
  74.  
  75.     [DataContract]
  76.     public class ProductText
  77.     {
  78.         [DataMember]
  79.         public string Code { get; set; }
  80.         public string Field { get; set; }
  81.         public string Value { get; set; }
  82.  
  83.     }
  84.  
  85.     public override void CreateNewOutputRows()
  86.     {
  87.         JavaScriptSerializer serialize = new JavaScriptSerializer();
  88.         serialize.MaxJsonLength = int.MaxValue;
  89.  
  90.         WebRequest request = WebRequest.Create(URL);
  91.         request.Method = "GET";
  92.         request.Timeout = 10000000;
  93.  
  94.         WebResponse webresponse = request.GetResponse();
  95.  
  96.         Console.WriteLine(((HttpWebResponse)webresponse).StatusDescription);
  97.         Stream dataStream = webresponse.GetResponseStream();
  98.  
  99.  
  100.        
  101.  
  102.         using (var streamReader = new StreamReader(dataStream))
  103.         {
  104.             try
  105.             {
  106.  
  107.                 string json = streamReader.ReadToEnd();
  108.                 List<Article> c = serialize.Deserialize<List<Article>>(json);
  109.                 //MessageBox.Show(c.Count.ToString());
  110.                 foreach (Article co in c) {
  111.                     Output0Buffer.AddRow();
  112.                     Output0Buffer.ArticleNumber = co.ProductNo;
  113.                    // Output0Buffer.Name = co.Description;
  114.                     Output0Buffer.Price = co.Price;
  115.  
  116.                  //   test1 = co.Category;
  117.                  //   test2 = co.DiscountSalesCode;
  118.                 //    test3 = co.SupplierNo;
  119.  
  120.                     if (co.ProductText != null)
  121.                     {
  122.                         string secondlabel = "";
  123.                         foreach (var item in co.ProductText)
  124.                         {
  125.                             if (item.Code == "f")
  126.                             {
  127.                                 secondlabel = item.Value;
  128.                             }
  129.  
  130.                         }
  131.                         if (secondlabel != "")
  132.                         {
  133.                             Output0Buffer.Name = co.Description + " " + secondlabel;
  134.                         }
  135.                         else
  136.                         {
  137.                             Output0Buffer.Name = co.Description;
  138.                         }
  139.                     }
  140.  
  141.                     else
  142.                     {
  143.                         Output0Buffer.Name = co.Description;
  144.                     }
  145.                    
  146.                     //for (int i = 0; i < co.ProductText.Count; i++)
  147.                     //{
  148.                     //    extraText = "Hi";
  149.                         //if (co.ProductText[i].Code == "f")
  150.                         //{
  151.                         //    extraText = co.ProductText[i].Value;
  152.                         //}
  153.  
  154.                    // }
  155.  
  156.  
  157.                    
  158.                    
  159.  
  160.                     if (!int.TryParse(co.Category, out temp))
  161.                     {
  162.                         Output0Buffer.Category = 0;
  163.                     }
  164.                     else
  165.                     {
  166.                         Output0Buffer.Category = Convert.ToInt32(co.Category);
  167.                     }
  168.                    
  169.                     Output0Buffer.Quantity = co.AmountPerUnit;
  170.                     if (!int.TryParse(co.DiscountSalesCode, out temp))
  171.                     {
  172.                         Output0Buffer.Discount = 0;
  173.                     }
  174.                     else
  175.                     {
  176.                         Output0Buffer.Discount = Convert.ToInt32(co.DiscountSalesCode);
  177.                     }
  178.                     if (!int.TryParse(co.SupplierNo, out temp))
  179.                     {
  180.                         Output0Buffer.LevNumber = 0;
  181.                     }
  182.                     else
  183.                     {
  184.                         Output0Buffer.LevNumber = Convert.ToInt32(co.SupplierNo);
  185.                     }
  186.                     Output0Buffer.CostPrice = co.PurchasePrice;
  187.                     Output0Buffer.Currency = co.PurchaseCurrencyCode;
  188.                     Output0Buffer.StandardPrice = co.StandardPrice;
  189.                     Output0Buffer.TK = co.OriginType;
  190.                     Output0Buffer.SupplierProductNo = co.SupplierProductNo;
  191.  
  192.                 }
  193.                
  194.             }
  195.  
  196.             catch (Exception e)
  197.             {
  198.              //   System.Windows.Forms.MessageBox.Show(test1);
  199.               //  System.Windows.Forms.MessageBox.Show(test2);
  200.               //  System.Windows.Forms.MessageBox.Show(test3);
  201.                 System.Windows.Forms.MessageBox.Show(e.ToString());
  202.                 Console.WriteLine(e);
  203.             }
  204.  
  205.             streamReader.Close();
  206.             dataStream.Close();
  207.             webresponse.Close();
  208.  
  209.         }
  210.  
  211.  
  212.        
  213.  
  214.    
  215.  
  216.  
  217.     }
  218.  
  219.  
  220. }
  221.