简体   繁体   中英

Change data type within a recordset by VB.NET or C#

I'm trying to change the data type with an SSIS recordset. I got the record from an Excel file using SSIS and save it into an object variable, 'User::CriteriaResult'.

When I try to use a foreach loop to loop through it, I get an error:

Error: 0xC001F009 at Get excel data: The type of the value being assigned to variable "User::CriteriaID" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.

The variable "User::CriteriaID" is defined as string, however, my data in the recordset is like this: 1,2,3A,3B,...

在此处输入图片说明

I try to use a script task to change the data type within the recordset and save it back to the recordset 'User::CriteriaResult'. The code I use it like this:

C#:

using System.Data.OleDb;
DataTable dt = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dt, Dts.Variables["User::CriteriaResult"].Value);

VB.NET:

Imports System.Data.OleDb
Dim oleDA As New OleDbDataAdapter
Dim dt As New DataTable
Dim row As DataRow
oleDA.Fill(dt, Dts.Variables("User::CriteriaResult").Value)

My concept is to create a new datatable, copy data from the recordset and change it to string, then store it back to the recordset and overwrite the old one. But I'm not quite sure how to do so.

Something I have tried:

For Each row In dt.Rows
        MsgBox(row.ToString)
    Next row

在此处输入图片说明

I got a bunch of rows instead of a row value. Also, I don't know how to save my revised records back to the recordset.

Could you give me any suggestions? Either in VB or C# could both help me a lot.

in VB.net

 Private Sub GetValues()
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    da.Fill(dt, "Your record set") 'not in quotes
    Dim row As DataRow
    Dim index As Integer = 0 ' index of the column you want

For Each row In dt.Rows
     Dim result As String = row(index).ToString
     Debug.Print(result)
Next

End Sub

in C#

private void GetValues()
{
    OleDbDataAdapter da = new OleDbDataAdapter();
    DataTable dt = new DataTable();
    da.Fill(dt, "Your recordset");//not in  quotes
    Int32 index = 0; //index of the column you need
    foreach (DataRow row in dt.Rows)
        {
            String result = row[index].ToString();
            Debug.Print(result);
        }
}

Try looping over columns and change the data type for the whole column values, you can use the following code (using VB.Net) :

Imports System.Data.OleDb

Dim oleDA As New OleDbDataAdapter
Dim dt As New DataTable
Dim row As DataRow
oleDA.Fill(dt, Dts.Variables("User::CriteriaResult").Value)


For each dCol as DataColumn in dt.Columns

    dCol.DataType = "".GetType()

Next

Dts.Variables("User::CriteriaResult").Value = dt

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