How to access Object SSIS variable inside script task? - c#

I have SSIS variable of type object which holds the OLE DB result set from OLE DB source.
The object is list of blogs with each blog having attributes title and description.
How can I parse the SSIS object variable and iterate over each blog and then field?
EDIT :
I want to read full result set in script task.

// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::SSISObjectVariableFromPackage"].Value);
// for test data
DataTable sourceTable = dt;
//Now loop through databale and do your processing
//----------------------------------------------------------------------
//Updated -- possible solution to fix issue in comments but left original above as that works for most
//try this version instead, mabye converting the SSIS variable to a C# object first, and not accessing it directly into the A.Fill may resolve your issue in the comments:
Object OBJDataTableValidFieldListFull = Dts.Variables["User::SSISObjectVariableFromPackage"].Value;
// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, OBJDataTableValidFieldListFull);
// for test data
DataTable sourceTable = dt;
//Now loop through databale and do your processing

Related

How to use a SSIS Dataset inside Dataflow Script component for Source?

I'm looking for a way to use my Dataset variable in Script component of SSIS data flow task, here is what I tried till now
My First Task is Execute SQL task (CData PostgreSQL Task)
This would run a Postgre query and store the result in a variable User::resultSet which is of the data type DataSet
My Second Task is Data Flow Task
In my second task I wish to use the dataset as my source. I tried to achieve this by using Script Component (Source)
My Script
public override void CreateNewOutputRows()
{
DataTable dt = (DataSet)Variables.resultSet;
// The DataTable is now ready to use! No more recordset quirks.
foreach (DataRow dr in dt.Rows)
{
// Add a new output row for this transformed data.
OrdersBuffer.AddRow();
// Now populate the columns
OrdersBuffer.id = int.Parse(dr["id"].ToString());
OrdersBuffer.created = int.Parse(dr["created"].ToString());
OrdersBuffer.modified = int.Parse(dr["modified"].ToString());
}
}
Issue
In my Script, I cant figure out a way to convert the dataset into a datatable,
I believe the problem line is DataTable dt = (DataSet)Variables.resultSet; in my script
I also tried DataTable dt = resultSet.Tables[0]; & DataTable dt = resultSet.Table[0]; but all them are throwing syntax error.
Any lead or guidance will be really appreciated.
Try this one
DataTable dt = (Variables.resultSet as DataSet).Tables[0];
May throw an exception if type cast is not possible.
I have not used the dataset variable type but I have dont this with an OBJECT type variable and MS SQL DB. If you can change to use object variable this should work for you (or maybe if you cant you can see how I am passing the OBJECT variable and update your code to access the variable in similar way.
First I run a SQL task that has a result set as output in SSIS to my OBJECT variable data type. Then I pass it to my script task like this:
Inside the C# Script task I have this below code to access the OBJECT variable and then covert it to a data table that I use later on
Where this variable is my object variable type in the SSIS Package:
User::ObjDataToSaveToExportFile
// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::ObjDataToSaveToExportFile"].Value);
// for test data
//DataTable sourceTable = GetTestData();
DataTable sourceTable = dt;
You can use below approach of using DataAdapter to fill datatable. Reference Article
public override void CreateNewOutputRows()
{
// Set up the DataAdapter to extract the data, and the DataTable object to capture those results
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
// Extract the data from the object variable into the table
da.Fill(dt, Variables.resultSet);
// Since we know the column metadata at design time, we simply need to iterate over each row in
// the DataTable, creating a new row in our Data Flow buffer for each
foreach (DataRow dr in dt.Rows)
{
// Add a new output row for this transformed data.
OrdersBuffer.AddRow();
// Now populate the columns
OrdersBuffer.id = int.Parse(dr["id"].ToString());
OrdersBuffer.created = int.Parse(dr["created"].ToString());
OrdersBuffer.modified = int.Parse(dr["modified"].ToString());
}

Clone DataTable from One Dataset to DataTable of Other DataSet

I am trying to clone Datable from one dataset to datatable from other Dataset, but it does not work.
Dataset ds = new Dataset();
ds.Tables.Add("key");
ds.Table["key"] = cust.Tables[0].Clone(); // This line is not working
Above code gives below error
Property or indexer 'System.Data.DataTableCollection.this[int]' cannot be assigned to — it's read only
cust is also a Dataset. Can someone help me resolve this
The error you are getting is similar to this:
Property or indexer 'System.Data.DataTableCollection.this[int]' cannot
be assigned to -- it is read only
You are doing it right once, and then you are doing it wrong in the line after.
The error is telling you, that cust.Tables[0] is read only. You cannot assign any values to it, so you have to use the Add() method:
DataSet cust = new DataSet(); // create source DataSet
cust.Tables.Add("sourceTable"); //add table to source
DataSet ds = new DataSet(); // create target DataSet
ds.Tables.Add((cust.Tables[0]).Clone()); //clone source table
(ds.Tables[0]).TableName = "keys"; // assign name
You need to wrap ds.Tables[indexer] in paranthesis () so the compiler treats it as a datatable.
You should assign a DataTable variable to keep the readability:
var sourceTable = cust.Tables[0];
/* ... */
ds.Tables.Add(sourceTable.Clone());

DataTable with existing Schema not Importing DataRow of same Schema

Greets! I am having problems importing a row I created into a DataTable that resides in a DataSet. I pre-populate the "newDataSet" from a SQL Database that is empty but it does contain Tables with a Schema already set up. I have verified that the DataTables in "newDataSet" are getting the Schema imported to them.
Everything looks right as there is no error logs, but no datarow is ever added. Both my Console.WriteLine report back the same Count.
Thank you for taking the time to review this. I appreciate you.
Initial Setup:
var DataSet newDataSet = new DataSet("foo"); // A SQL Adapater was used to fill this from a pre existing Database.
var checkDataSet = new DataSet();
var checkDataTable = new DataTable();
Cloning the DataSet and DataTable.
checkDataSet = newDataSet.Clone();
checkDataTable = checkDataSet.Tables["moreFoo"].Clone();
Creating the DataRow:
var newDataRow = checkDataTable.NewRow();
Filling the Columns in the DataRow:
newDataRow[0] = obj1;
newDataRow[1] = obj2;
newDataRow[2] = obj3;
Importing the DataRow to the "newDataSet" DataTable:
Console.WriteLine(newDataSet.Tables["moreFoo"].Rows.Count.ToString());
newDataSet.Tables["moreFoo"].ImportRow(newDataRow);
Console.WritelLine(newDataSet.Tables["moreFoo"].Rows.Count.ToString());
The answer is:
newDataSet.Tables["moreFoo"].ImportRow(newDataRow);
should be:
newDataSet.Tables["moreFoo"].Rows.Add(newDataRow.ItemArray);

DataTable From Session Memory has No Columns

Im trying to retrieve a datatable that I put into a session variable, however when i do it apears that there are no columns...
I have done this before in VB.NET, I'n now using C#, and it worked perfectly, and as far as i can see there is no change in the code other than the obvious syntax changes.
EDIT:
The dt (in part 2) variable has the right data when i look in the data visulization window, but i have noticed that the other properties associated with a datatable are abscent. When i hover over a normal looking datatable with my mouse it looks like the following: " dt ----- {System.Data.DataTable}" but in the class im working on it just looks like "dt ----- {}". Also, after I return the session variable into the dt I clone it (dtclone = dt.clone(); ) and the clone is empty in the data visulizer.... what on earth!
EDIT 2:
I have now also tried converting the first datatable to a dataset, putting this in the session variable, and recoverting it back to a datatable in the class.
Am starting to wonder if it is a probelm with:
dt.Load(sqlReader);
The data does appear after this step though in the dataset visualiser, but not after being cloned.
Code below.
All help very welcome!
Thanks in advance
Chris
1) SQL command in a webhandler, the results of which populate the datatable to be put into the session variable.
DataTable dt = new DataTable();
SqlDataReader sqlReader= default(SqlDataReader);
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlReader = storedProc.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(sqlReader);
System.Web.HttpContext.Current.Session["ResultsTable"] = dt;
2) Part of the code in a class which performs calculations on the table:
DataTable dt = (DataTable)HttpContext.Current.Session["ResultsTable"];
did you call DataTable dispose after setting Session Var?
Because if you do this, solution is like this:
System.Web.HttpContext.Current.Session["ResultsTable"] = dt.Copy();

Storing and retrieving datatable from session

How to store a datatable in session and to retrieve the values from the session in c#.net?
Add a datatable into session:
DataTable Tissues = new DataTable();
Tissues = dal.returnTissues("TestID", "TestValue");// returnTissues("","") sample function for adding values
Session.Add("Tissues", Tissues);
Retrive that datatable from session:
DataTable Tissues = Session["Tissues"] as DataTable
or
DataTable Tissues = (DataTable)Session["Tissues"];
To store DataTable in Session:
DataTable dtTest = new DataTable();
Session["dtTest"] = dtTest;
To retrieve DataTable from Session:
DataTable dt = (DataTable) Session["dtTest"];
You can do it like that but storing a DataSet object in Session is not very efficient. If you have a web app with lots of users it will clog your server memory really fast.
If you really must do it like that I suggest removing it from the session as soon as you don't need the DataSet.
this is just as a side note, but generally what you want to do is keep size on the Session and ViewState small. I generally just store IDs and small amounts of packets in Session and ViewState.
for instance if you want to pass large chunks of data from one page to another, you can store an ID in the querystring and use that ID to either get data from a database or a file.
PS: but like I said, this might be totally unrelated to your query :)
A simple solution for a very common problem
// DECLARATION
HttpContext context = HttpContext.Current;
DataTable dt_ShoppingBasket = context.Session["Shopping_Basket"] as DataTable;
// TRY TO ADD rows with the info into the DataTable
try
{
// Add new Serial Code into DataTable dt_ShoppingBasket
dt_ShoppingBasket.Rows.Add(new_SerialCode.ToString());
// Assigns new DataTable to Session["Shopping_Basket"]
context.Session["Shopping_Basket"] = dt_ShoppingBasket;
}
catch (Exception)
{
// IF FAIL (EMPTY OR DOESN'T EXIST) -
// Create new Instance,
DataTable dt_ShoppingBasket= new DataTable();
// Add column and Row with the info
dt_ShoppingBasket.Columns.Add("Serial");
dt_ShoppingBasket.Rows.Add(new_SerialCode.ToString());
// Assigns new DataTable to Session["Shopping_Basket"]
context.Session["Shopping_Basket"] = dt_PanierCommande;
}
// PRINT TESTS
DataTable dt_To_Print = context.Session["Shopping_Basket"] as DataTable;
foreach (DataRow row in dt_To_Print.Rows)
{
foreach (var item in row.ItemArray)
{
Debug.WriteLine("DATATABLE IN SESSION: " + item);
}
}

Categories

Resources