How to store and retreive DICOM files in SQL Server using C#
===

My objective is to store DICOM files in SQL Server with some ID, and when user wants that DICOM file he can download from the SQL Server using its corresponding ID.  The file should not change its originality while storing into the server and also when retrieving from the SQL Server. I am using `Varbinary(max)` data-type to store the byte array of the DICOM file in SQL Server. I am converting the memory stream of the DICOM file into byte array and then storing into the SQL server as mentioned below:

     using (MemoryStream memStream = new MemoryStream())
     {
        using (FileStream fileStream = File.Open(txtDICOMFilePath.Text, FileMode.Open))
        {
             // Copy the file stream to memory stream.
             fileStream.CopyTo(memStream);
        }
        int intL = Convert.ToInt32(memStream.Length);
        byte[] objData = new byte[intL];
     }
    
     //Set insert query
     string qry = "insert into ImagesStore (ID,ImageData) values(@ID, @ImageData)";
        
     //Initialize SqlCommand object for insert.
     SqlCommand SqlCom = new SqlCommand(qry, CN);
     
     //We are passing Original Image Path and Image byte data as sql parameters.
     SqlCom.Parameters.Add(new SqlParameter("@ID", (object)txtUniqueID.Text));
     SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)objData));
        
     //Open connection and execute insert query.
     CN.Open();
     SqlCom.ExecuteNonQuery();
     CN.Close();

**Problem:**
While storing the Image it stores correctly(Without any exception) but while retrieving it not gives the exact data. can any body help me in storing the DICOM file in SQL server without any loss ?

Any help will be appreciated. Thanks in advance..