0

This is question is related to answer of Can Sql Server BULK INSERT read from a named pipe/fifo?

@Grimace_of_Despair provided a solution using C#. See the GitHub project for more details.

  1. Create a Named Pipe with server name = .

    public NamedPipe(string name, string server, string dataValue)
    {
             _dataValue = dataValue;
    
             Name = name;
             Server = server;
             Stream = new NamedPipeServerStream(name, PipeDirection.Out, 2,
                                                PipeTransmissionMode.Byte, 
                                                PipeOptions.Asynchronous);
             Stream.BeginWaitForConnection(OnConnection, this);
    }
    
  2. Bulk Insert using Named Pipe

    var insertCommand = DbConnection.CreateCommand();
    
    insertCommand.CommandText = "BULK INSERT [MyTable] FROM '\\.\pipe\mypipe' WITH (FORMATFILE='c:\path\to\formatfile')";
    insertCommand.ExecuteNonQuery();
    

Above bulk insert command works fine when I am bulk inserting on the same server and using \\\\.\pipe\mypipe

When I replace \\\\.\pipe\mypipe with \\\\LocalMachineName\pipe\mypipe, I get an error:

System.Data.SqlClient.SqlException (0x80131904): Bulk load: An unexpected end of file was encountered in the data file.

Does anyone know the reason for this error and how to fix it?

Community
  • 1
  • 1
Swapnil
  • 424
  • 2
  • 13
  • Can you inspect the contents of the named pipe in the receiver code? – Nick.Mc Aug 24 '15 at 11:15
  • Named pipe contains delimited data which i am trying to bulk insert into sql table. Bulk insert runs successfully when I refer pipe as **\\\\.\pipe\mypipe**. It throws an error when I use **\\\\LocalMachineName\pipe\mypipe**. – Swapnil Aug 24 '15 at 12:19
  • I know. Can you inspect the contents if the pipe, i.e. get the length of the data? Save the data as a text file? Then you can inspect the data _before_ you try to BULK INSERT it. – Nick.Mc Aug 25 '15 at 01:31
  • I stored data in a file instead of named pipe. I was able to bulk insert from this file. Is there any permission issue with named pipe which is not allowing me to access it as **\\\\LocalMachineName\pipe\mypipe** – Swapnil Aug 25 '15 at 09:14
  • Did you load this file from the named pipe first? I'm just suggesting: you need to see the data in the named pipe to help you understand the error. Break the issue into smaller pieces. Also perhaps try synchronous instead of asynchronous. – Nick.Mc Aug 25 '15 at 09:22
  • As stated earlier, contents of named pipe are correct as I am able bulk insert when I refer it as **\\.\pipe\mypipe** – Swapnil Aug 25 '15 at 11:24

0 Answers0