Some time we need to call the SP using the Script task in SSIS.
In this demo
we will see how we can call the SP in the script task and generate the CSV report.
We are
calling the below SP
DB: AdventureWorks2012
Sp
Create Procedure [dbo].[P_get_emp_details] @year INT |
And writing
the below c# code.
public void Main() { // TODO: Add your code here using (SqlConnection conn
= new SqlConnection("Data Source=BAGESH-PC\\BAGESHDB;Initial
Catalog=AdventureWorks2012;Integrated Security=true")) { SqlCommand sqlComm
= new SqlCommand("P_get_emp_details", conn);
sqlComm.Parameters.AddWithValue("@year", 10); da.SelectCommand = sqlComm; da.Fill(dt); } { writer.WriteLine("*****************Employee
Details Report generated on : " + DateTime.Now.ToString() + "**************"); s
= dt.Rows.Count.ToString(); foreach (DataRow row in dt.Rows) {
string
FirstName;
string
LastName;
string
JobTitle;
string
Gender;
object[]
array = row.ItemArray;
FirstName = array[0].ToString();
LastName = array[1].ToString();
JobTitle = array[2].ToString();
Gender = array[3].ToString();
writer.WriteLine(FirstName + "," + LastName + "," + JobTitle + "," + Gender); }
writer.WriteLine("Number of records
written in this file : " + s);
writer.WriteLine("*****************
End of the File *****************"); }
} |
See the report.
Report data.
Report
generated successfully.