Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. public void Main()
  2. {
  3.  
  4.  
  5. string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
  6. try
  7. {
  8.  
  9. string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
  10. string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
  11. string StoredProcedureFig1_1 = Dts.Variables["User::StoredProcedureFig1_1"].Value.ToString();
  12. string StoredProcedureFig1_2 = Dts.Variables["User::StoredProcedureFig1_2"].Value.ToString();
  13. string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
  14. string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
  15.  
  16.  
  17.  
  18. //USE ADO.NET Connection from SSIS Package to get data from table
  19. SqlConnection myADONETConnection = new SqlConnection();
  20. myADONETConnection = (SqlConnection)(Dts.Connections["localhost.onramps_tacc"].AcquireConnection(Dts.Transaction)
  21. as SqlConnection);
  22.  
  23.  
  24. //Execute Stored Procedure and save results in data table
  25. string query1 = "EXEC " + StoredProcedureFig1_1;
  26. //how to run below query too ? Stackoverflow question
  27. string query2 = "EXEC " + StoredProcedureFig1_2;
  28. SqlCommand cmd = new SqlCommand(query1, myADONETConnection);
  29. DataTable d_table = new DataTable();
  30.  
  31. d_table.Load(cmd.ExecuteReader());
  32.  
  33. myADONETConnection.Close();
  34.  
  35. string FileFullPath = DestinationFolder + "\" + FileNamePart + "_" + datetime + FileExtension;
  36.  
  37. StreamWriter sw = null;
  38. sw = new StreamWriter(FileFullPath, false);
  39. for (int i = 0; i < 2; i++)
  40. {
  41. // Write the Header Row to File
  42. String var = d_table + "i";
  43. int ColumnCount = var.Columns.Count;
  44. for (int ic = 0; ic < ColumnCount; ic++)
  45. {
  46. sw.Write(d_table.Columns[ic]);
  47. if (ic < ColumnCount - 1)
  48. {
  49. sw.Write(FileDelimiter);
  50. }
  51. }
  52. sw.Write(sw.NewLine);
  53.  
  54. // Write All Rows to the File
  55. foreach (DataRow dr in d_table.Rows)
  56. {
  57. for (int ir = 0; ir < ColumnCount; ir++)
  58. {
  59. if (!Convert.IsDBNull(dr[ir]))
  60. {
  61. sw.Write(dr[ir].ToString());
  62. }
  63. if (ir < ColumnCount - 1)
  64. {
  65. sw.Write(FileDelimiter);
  66. }
  67. }
  68. sw.Write(sw.NewLine);
  69.  
  70. }
  71. }
  72. sw.Close();
  73.  
  74. Dts.TaskResult = (int)ScriptResults.Success;
  75. }
  76. catch (Exception exception)
  77. {
  78. // Create Log File for Errors
  79. using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\" +
  80. "ErrorLog_" + datetime + ".log"))
  81. {
  82. sw.WriteLine(exception.ToString());
  83. Dts.TaskResult = (int)ScriptResults.Failure;
  84. }
  85. }
  86.  
  87. Dts.TaskResult = (int)ScriptResults.Success;
  88. }
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement