SQL Bulkcopy YYYYMMDD problem

后端 未结 3 1786
后悔当初
后悔当初 2020-12-12 00:40

I have a String to Date conversion problem using SQL Bulkcopy in asp.net 3.5 with C#

I read a large CSV file (with CSV reader). One of the strings read should be loa

3条回答
  •  [愿得一人]
    2020-12-12 00:47

    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 CSV
    DateTime thedate = 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 read
    long records_read = 0;
    While(reader.Read())
    {
        //We will take rows in a Buffer of 50 records
        int 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...

提交回复
热议问题