Db2

Db2

Where DBAs and data experts come together to stop operating and start innovating. Connect, share, and shape the AI era with us.


#Data


#Data
#Databases
#Operatingsystems
#Db2
#Databasesolutions
 View Only

Problem Loading Byte Data Type into table with .Net and IBM Data Server Provider for .NET6

  • 1.  Problem Loading Byte Data Type into table with .Net and IBM Data Server Provider for .NET6

    Posted 03/06/23 09:54 AM

    I'm running with version 6.0.0.300 of the above and connecting to a DB2 Server on Windows Version 11.5. The following code shows the problem:

    using IBM.Data.Db2;
    using System.Data;
    
    var con = new DB2Connection("your connection string");
    con.Open();
    var dt = new DataTable("input");
    dt.Columns.Add("INPUT_COL", typeof(byte));
    dt.Rows.Add(6);
    
    var copy = new DB2BulkCopy(con);
    copy.DestinationTableName = "Table Name";
    copy.ColumnMappings.Add("INPUT_COL", "NUMBER_COL");
    copy.WriteToServer(dt);
    con.Close();

    This will throw an exception: System.InvalidCastException: 'Unable to cast object of type 'System.Byte' to type 'System.Byte[]'.'

    However, if I simply change the 

    dt.Columns.Add("INPUT_COL", typeof(byte));

    Line to:

    dt.Columns.Add("INPUT_COL", typeof(short));

    Then it works as expected. My application (which supports multiple databases), however, allows users to select any data type including byte as a column type. The underlying Table has a single column of type SMALLINT.



    ------------------------------
    Colin Paterson
    ------------------------------