Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Main()
- {
- string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
- try
- {
- string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
- string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
- string StoredProcedureFig1_1 = Dts.Variables["User::StoredProcedureFig1_1"].Value.ToString();
- string StoredProcedureFig1_2 = Dts.Variables["User::StoredProcedureFig1_2"].Value.ToString();
- string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
- string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
- //USE ADO.NET Connection from SSIS Package to get data from table
- SqlConnection myADONETConnection = new SqlConnection();
- myADONETConnection = (SqlConnection)(Dts.Connections["localhost.onramps_tacc"].AcquireConnection(Dts.Transaction)
- as SqlConnection);
- //Execute Stored Procedure and save results in data table
- string query1 = "EXEC " + StoredProcedureFig1_1;
- //how to run below query too ? Stackoverflow question
- string query2 = "EXEC " + StoredProcedureFig1_2;
- SqlCommand cmd = new SqlCommand(query1, myADONETConnection);
- DataTable d_table = new DataTable();
- d_table.Load(cmd.ExecuteReader());
- myADONETConnection.Close();
- string FileFullPath = DestinationFolder + "\" + FileNamePart + "_" + datetime + FileExtension;
- StreamWriter sw = null;
- sw = new StreamWriter(FileFullPath, false);
- for (int i = 0; i < 2; i++)
- {
- // Write the Header Row to File
- String var = d_table + "i";
- int ColumnCount = var.Columns.Count;
- for (int ic = 0; ic < ColumnCount; ic++)
- {
- sw.Write(d_table.Columns[ic]);
- if (ic < ColumnCount - 1)
- {
- sw.Write(FileDelimiter);
- }
- }
- sw.Write(sw.NewLine);
- // Write All Rows to the File
- foreach (DataRow dr in d_table.Rows)
- {
- for (int ir = 0; ir < ColumnCount; ir++)
- {
- if (!Convert.IsDBNull(dr[ir]))
- {
- sw.Write(dr[ir].ToString());
- }
- if (ir < ColumnCount - 1)
- {
- sw.Write(FileDelimiter);
- }
- }
- sw.Write(sw.NewLine);
- }
- }
- sw.Close();
- Dts.TaskResult = (int)ScriptResults.Success;
- }
- catch (Exception exception)
- {
- // Create Log File for Errors
- using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\" +
- "ErrorLog_" + datetime + ".log"))
- {
- sw.WriteLine(exception.ToString());
- Dts.TaskResult = (int)ScriptResults.Failure;
- }
- }
- Dts.TaskResult = (int)ScriptResults.Success;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement