none
How to read csv file from Azure blob to DataTable or DataSet using C#? RRS feed

  • Question

  • Hi Techie!

    I have a requirement where i need to download csv file from blob and in that, i need to fetch one particular column values, with that value i have to fetch matching rows from D365.

    So, for this, I tried getting the data from Blob and loading the same to DataTable, but i'm getting error in the last line below,

                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container = blobClient.GetContainerReference(strBlobContainerName);
                    CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(strInputFileName); 

                    using (var memoryStream = new MemoryStream())
                    {

                        long fileByteLength = blockBlob2.Properties.Length;
                        byte[] fileContent = new byte[fileByteLength];
                        await blockBlob2.DownloadToByteArrayAsync(fileContent, 0);
                        
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
                        var mem = new MemoryStream(fileContent);
                        DataTable dt = (DataTable)formatter.Deserialize(mem);

                     }

    Error: 

    The input stream is not a valid binary format. The starting contents (in bytes) are: 74-73-69-5F-6E-6F-2C-61-67-65-6E-63-79-2C-74-73-69 ...

    Please suggest me an approach with which i can load csv file from Blob to DataTable.

    Thanks


    Santhosh Kumar K

    Wednesday, November 13, 2019 9:50 AM

All replies

  • @SanthoshKumar09 Have you tried serializing (Saving) with BinaryFormatter, and reading again with BinaryFormatter. 

    Can you try to serializing and deserializing with different formatters?

    Just for clarification: Try to refer to the suggestion mentioned in the SO thread. If the issue still persist share the screenshot of the error message

    Kindly let me know the status.

    ------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.

    Thursday, November 14, 2019 4:52 AM
    Moderator
  •  Just checking in to see if the above answer helped. If this answers your query, do click “Mark as Answer” and Up-Vote for the same, which might be beneficial to other community members reading this thread. And, if you have any further query do let us know.

    Friday, November 15, 2019 7:42 AM
    Moderator
  • @SanthoshKumar09 Is there any update on the issue?

    If the suggested answer helped for your issue, do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

    Monday, November 18, 2019 5:30 AM
    Moderator
  • Following up to see if the above suggestion was helpful. And, if you have any further query do let us know.  Just checking in to see if the above answer helped. If this answers your query, do click “Mark as Answer” and Up-Vote for the same, which might be beneficial to other community members reading this thread

    Thursday, November 21, 2019 9:04 AM
    Moderator