Question

I am writing a small utility to copy a database from a proprietary ODBC database into a SQL Server database. Everything is working great, except when I use SQLBulkCopy to copy over the data. It works in most cases, but not when it's trying to copy a TIME field into a SQL DATETIME. I get this error:

The given value of type TimeSpan from the data source cannot be converted to type datetime of the specified target column.

When I create the schema in SQL Server I have to make the DATE and TIME fields DATETIME types in SQL Server, so there is no way around this. I wanted to use SQLBulkCopy so I didn't have to manually read through every record in every table and wrap logic around the special cases. Before I go down that road, is there another way I can do this? I have no control at all on the source database schema or content.

Was it helpful?

Solution

I assume you are dealing with pre-SQL Server 2008. SQL Server 2008 has DATE and TIME data types.

I think you would have to use a DataTable which matched the SQL Server schema and load this from your source reader, appropriately changing any TIME to a DATETIME by adding date information (e.g. 1/1/1900). Then use WriteToServer(DataTable). You might want to do it in batches, since you may use a bunch of memory reading it all into a DataTable.

Any particular reason you can't use SSIS?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top