简体   繁体   中英

Bulk Insert into .CSV destination file using c# in SSIS script task

I have a requirement for inserting a bunch of SQL Server tables data into .CSV files dynamically, I am using for each loop and Script task in control flow to write the data into CSV files, currently, I am reading row by row from source and writing it to the CSV file, which is taking much time, I am looking for Bulk insert option to write from SQL server to .CSV file destination. I did spend some time in researching about it but all I found was SqlBulkCopy class option, it works only if the destination is SQL Server table, Can you please help me with an approach that works for SQL server table as source and CSV file as a destination.

You can try to export SQL Server data to CSV by using the bcp Utility

在此处输入图片说明

bcp "query" queryout data_file -S server_name[\instance_name] -U login_id -P password -c

Example:

bcp "EXEC MyDatatabase.dbo.MyTable" queryout "C:\DataBCP.csv" –c –t , –S (local) –T

You can call bcp utility using System.Diagnostics.Process() in your C# code:

    Process p = new System.Diagnostics.Process();
    ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo();
    pi.FileName = "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\bcp.exe";
    pi.Arguments = "EXEC MyDatatabase.dbo.MyTable\" queryout \"C:\\DataBCP.csv\" –c –t , –S (local) –T";
    p.StartInfo = piOriginal;
    pi.UseShellExecute = false;
    p.Start();
    p.WaitForExit();
    p.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM