Connection String in Script Task in SSIS

Connection String in Script Task in SSIS

Create ADO.net connection in Connection Managers Tab

(That is .net Providers\ SqlClient Data Provider)
(Connection String: Data Source= Server Name;Initial Catalog= Data base Name;Integrated Security=True;)

using System.Data.SqlClient;


SqlConnection Connection = (SqlConnection)Dts.Connections["Name of Connection manager(Ado connection)"].AcquireConnection(null);
SqlCommand sqlCommand = new SqlCommand();
if (Connection.State == ConnectionState.Closed)
    Connection.Open();
sqlCommand.Connection = Connection;
sqlCommand.CommandText = "Write query here or give Stored procedure";
sqlCommand.CommandType = CommandType.StoredProcedure; // give type if it is stored procedure(SP)
sqlCommand.Parameters.AddWithValue("Parameter1", DbType.String).Value = variableName1.Trim(); // passing input parameter into  SP
sqlCommand.Parameters.AddWithValue("EParameter2", DbType.String).Value = variableName2.Trim();

  SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  DataSet ds1 = new DataSet();
  sqlDataAdapter.Fill(ds1);
  Dts.Connections["Name of Connection manager(Ado connection)"].ReleaseConnection(null);
  Connection.Close();
  sqlCommand.Dispose();

No comments:

Post a Comment