Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Sybase Bulk Copy via .net DLL: Value does not fall within the expected range

I am trying to migrate data from Sql Server to a Sybase 16.0 database using the .net DLLs supplied with the Sybase installation (Sybase.AdoNet4.AseClient.dll version 16.0.02).

To keep things simple I am trying to copy values from a table with a single INT column

--source table (MSSQL)

CREATE TABLE [dbo].[TO_INTS]

( [TO_INT] [int] NULL,

[TO_INT2] [int] NULL,

[NAME] [varchar](50) NULL,

[DT] [datetime] NULL )

ON [PRIMARY]

to

--target table (Sybase)

CREATE TABLE dbo.TO_INTS ( FROM_INT INT NOT NULL ) ON 'default'

I am using the code:

public void BulkCopyFromSqlServer(string sourceConnectionString, string targetConnectionString)

{

  SqlConnection sourceConnection = null;

  AseConnection targetConnection = new AseConnection(targetConnectionString);

  IDataReader dataSource=null;

  try

  {

  targetConnection.Open();

  MssqlCommand.GetDataReader(sourceConnectionString, out sourceConnection, out dataSource);  //see below

  AseBulkCopy blk = new AseBulkCopy(targetConnection);

  blk.BulkCopyTimeout = 1200;

  blk.DestinationTableName = "TO_INTS";

  blk.ColumnMappings.Clear();

  blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(0,0));

  blk.WriteToServer(dataSource);  // System.ArgumentException thrown here.

  blk.Close();

  }

  catch (AseException ex)

  {

  Console.WriteLine(ex.Message);

  }

  finally

  {

  sourceConnection.Dispose();

  targetConnection.Dispose();

  }

}

//MssqlCommand.GetDataReader(sourceConnectionString, out sourceConnection, out dataSource):

public static void GetDataReader(string sqlServerConnectionString, out SqlConnection conn,

out IDataReader reader)

{

  conn = new SqlConnection(sqlServerConnectionString);

  conn.Open();

  SqlCommand cmd = new SqlCommand("select * from TO_INTS", conn);

  cmd.CommandTimeout = 60;

  reader = cmd.ExecuteReader();

}

A System.ArgumentException is thrown at when WriteToServer() is called with the message "Value does not fall within the expected range". The stack trace is interesting in that it looks like the Sybase DLL cannot resolve the DB column name using the index provided in the mapping, which seems odd:

   at Sybase.Data.AseClient.AseBulkCopy.GetDBColName(String clientColName, Int32 clientColInx)

   at Sybase.Data.AseClient.AseBulkCopy.GenerateInsertCmdByReaderMetaInfo(DataTable rowFmt)

   at Sybase.Data.AseClient.AseBulkCopy.WriteToServer(IDataReader reader)

I have followed the same process for Sybase > Sql Server (pretty much line by line, but with the relevant DLLs switched) and this works.

Am I missing something obvious?

Thanks

Tags:
Former Member
Former Member replied

It looks like I have fixed this now.

The initial error arose because I did not know about putting the EnableBulkLoad parameter in the connection string.

Once that was added the second error arose because the table name was set using:

blk.DestinationTableName = "TO_INTS";

when it should have been:

blk.DestinationTableName = "dbo.TO_INTS";

Can I ask that you ask your engineering team to provide a more useful error message, such as "Table not found" or "Owner not specified"? That would have made debugging (once I'd added the EnableBulkLoad parameter to the connection string) easier.

Thanks all for your interest & assistance.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question