Guest User

Untitled

a guest
Jul 25th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public void Main()
  2. {
  3. try
  4. {
  5. String FilePath = Dts.Variables["$Package::DestinationFileName"].Value.ToString();
  6. String TableName = Dts.Variables["$Package::SourceTableName"].Value.ToString();
  7. String ConnStr = Dts.Variables["$Project::ConnStr_DataWarehouse"].Value.ToString();
  8.  
  9. //SqlConnection Conn = (SqlConnection)(Dts.Connections["DW"].AcquireConnection(Dts.Transaction) as SqlConnection);
  10. using (SqlConnection Conn = new SqlConnection(ConnStr))
  11. {
  12. String Sql = "SELECT * FROM " + TableName;
  13. if (File.Exists(FilePath))
  14. {
  15. try { File.Delete(FilePath); }
  16. catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); Dts.TaskResult = (int)ScriptResults.Failure; }
  17. }
  18. using (DataTable dt = new DataTable())
  19. {
  20. using (SqlCommand cmd = new SqlCommand(Sql, Conn))
  21. {
  22. Conn.Open();
  23. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  24. {
  25. da.Fill(dt);
  26. FileInfo newFile = new FileInfo(FilePath);
  27. using (ExcelPackage p = new ExcelPackage(newFile))
  28. {
  29. using (ExcelWorksheet ws = p.Workbook.Worksheets.Add("RejectetionReport"))
  30. {
  31. ws.Cells["A1"].LoadFromDataTable(dt, true);
  32. p.Save();
  33. }
  34. }
  35. }
  36. Conn.Close();
  37. }
  38. }
  39. }
  40.  
  41. Dts.TaskResult = (int)ScriptResults.Success;
  42. }
  43. catch (Exception ex)
  44. {
  45. MessageBox.Show(ex.Message.ToString());
  46. Dts.TaskResult = (int)ScriptResults.Failure;
  47. }
  48. }
Add Comment
Please, Sign In to add comment