mercredi 7 décembre 2016

Laravel Raw Bulk insert

I wrote a function in a Controller used in the Laravel Framework. The function gets the file path of an *.csv file and then inserts all elements of this *.csv file with a raw bulk insert statement into the database. The problem is that the elements are not put in the database table when the function is executed. I do not get any errors, too. When I execute the Query in SQL Server Manager it inserts the elements without problems. What am I doing wrong and is there a better way to bulk insert the elements of an csv file to the database?

Here is the code for the function:

        public static function bulkInsertCSV($filePath){

            $sql = "use [testDatabase] BULK INSERT [dbo].[testTable]   
                    FROM '" . $filePath . "' WITH (FIELDTERMINATOR = ';',"
                    . "ROWTERMINATOR = '\\n' );";
            //DB::statement($sql);
            DB::statement(DB::raw($sql));
        }

Best regards, Yalcin



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire