MS SQL Server - Bulk Insert Across a Network

前端 未结 5 577
北荒
北荒 2021-01-13 22:32

I have an application that uses MS SQL Server for which I\'ll need to do a bulk insert from a file. The sticking point is that the database and my application will be hoste

相关标签:
5条回答
  • 2021-01-13 23:04

    If the file is small enough then the ftp option may work (you will have a duplicate in your db box). However I don't see much of a problem to do option 1) if you have a gigabit network between the two with a single hop.

    0 讨论(0)
  • 2021-01-13 23:07

    I'm still looking for a way to do this in MS SQL, but what I did in MySQL was to save the CSV as a BLOB in a temporary table, run SELECT ... INTO DUMPFILE in a directory local to the DB server, then run the regular LOAD DATA statement on that local file (EDIT: I feel I should point out, this was before LOAD DATA LOCAL was available).

    I think spWriteStringToFile will do it.

    EDIT: I'm on to something.

    comm.CommandText = @"EXEC spWriteStringToFile @data, 'c:\datadumps', 'data.csv';
        BULK INSERT my_table FROM 'c:\datadumps\data.csv';";
    comm.Parameters.AddWithValue("data", File.ReadAllText(path));
    comm.ExecuteNonQuery();
    
    0 讨论(0)
  • 2021-01-13 23:08

    Actually you can place the file at both the Application and Database server.

    Both files must be the same path. From the Application server, the purpose is only for selection. The Database server is for the bulk insert.

    This is what I did for my customer and they also agreed with this simple trick.

    0 讨论(0)
  • 2021-01-13 23:18

    I've done it before, and tried both options.

    In the end, I did the opposite of choice 1. Share a directory on the DB server that the app can find. You don't have to deal with bandwidth issues during the bulk insert.

    The FTP server option works if you're particularly concerned with security or transferability.

    A final option (be very careful) is to use DTS with a localized SQL server. It might be more secure. If you do it wrong, it'll be much less efficient.

    0 讨论(0)
  • 2021-01-13 23:18

    I am not sure about 2k08 as there are some additional utilities to copy files between servers. FTP will be the faster of the two, because it doesn't use the window's file system to transfer the file. (there is overhead, but unless the file is large it might be negligible). There are other advantages to not using a share, such as the remote server with the file crashing mid-access.

    I disagree with cmartin about adding an open share to the db server. Generally you don't want to open file shares to the db server as it is generally considered a security risk, and a lot of places won't allow it. That being said it would negate you having transfer the file to another location to use the bulk import.

    0 讨论(0)
提交回复
热议问题