简体   繁体   中英

SSIS - Search string - Script task

I'd like to know if it's possible to input a query result as an array using a script task(C#)? I found a code that uses a script task but does not assing each value of a query result in a variable.

//Read list of Tables with Schema from Database
string query = "Select * From "+TableName;
SqlCommand cmd = new SqlCommand(query, myADONETConnection);
//myADONETConnection.Open();

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);

// Write the Header Row to File
int ColumnCount = d_table.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);
 }

Thanks.

Based on the following comment:

Basically, I'll make a query that returns multiple columns and rows and I'd like to assign each data in a variable, like a Foreach loop container. This code is an example

I will assume that you are looking to store a query result inside a table and loop over the result row by row and assign the columns value inside variable. Then you should use an Execute SQL Task to execute the query and store the ResultSet inside a variable of type System.Object then loop over the ResultSet (Result Table) using a ForEach Loop Container with an ADO enumerator .

To get a step by step guide you can refer to one of the following links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM