Sql Bulkcopy Yyyymmdd Problem
Solution 1:
If you can handel it in C# itself then this code will help get the date in the string as a DateTime object which you can pass directly
//datestring is the string read from CSVDateTimethedate= DateTime.ParseExact(dateString, "yyyyMMdd", null);
If you want it to be formatted as string then:
string thedate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
Good luck.
Update
In your scenario i don't know why date is not automatically formatted but from C# you need to get in and Interfere in the process of passing the data to the WriteToServer() method. Best i think you can do (keeping in mind the Performance) is to have a cache of DataRow items and Pass them to the WriteToServer() method. I will just write the sample code in a minute...
//A sample code.. polish it before implementation//A counter to track num of records readlong records_read = 0;
While(reader.Read())
{
//We will take rows in a Buffer of 50 recordsint i = records_read;//initialize it with the num of records last read
DataRow[] buffered_rows = new DataRow[50];
for(;i<50 ;i++)
{
//Code to initialize each rows with the data in the reader//.....//Fill the column data with Date properly formatted
records_read++;
reader.Read();
}
bcp.WriteToServer(buffered_rows);
}
Its not full code but i think you can work it out...
Solution 2:
Older issue, but wanted to add an alternative approach.
I had the same issue with SQLBulkLoader not allowing DataType/culture specifications for columns when streaming from IDataReader.
In order to reduce the speed overhead of constructing datarows locally and instead have the parsing occur on the target, a simple method I used was to temporarily set the thread culture to the culture which defines the format in use - in this case for US format dates.
For my problem - en-US dates in the input (in Powershell):
[System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-US'<callSQLBulkCopy>For your problem, you could do the same but since the date format is not culture specific, create a default culture object (untested):
CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "yyyyMMDD;
Thread.CurrentThread.CurrentCulture = newCulture;
I found allowing the database server to perform the type conversions once they've gotten through the SQLBulkCopy interface to be considerably faster than performing parsing locally, particularly in a scripting language.
Solution 3:
It's not entirely clear how you're using SqlBulkCopy, but ideally you shouldn't be uploading the data to SQL Server in string format at all: parse it to a DateTime or DateTimeOffset in your CSV reader (or on the output of your CSV reader), and upload it that way. Then you don't need to worry about string formats.
Post a Comment for "Sql Bulkcopy Yyyymmdd Problem"