Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Mp3 File want to save it in database as string by convert it to binary data how do that ??
i use windows application and will use access database for store files
Posted
Updated 9-Oct-10 21:57pm
v2
Comments
Hiren solanki 7-Oct-10 10:05am    
can you please specify your targeting database.

You can store them as BLOBs. However, you should consider storing the .mp3 files on the server and storing their paths in the database.
 
Share this answer
 
First you need to load the file into a stream

ASM
FileStream fileStream = new FileStream(@"d:\\Del\7\\SS 65.mp3", FileMode.Open);



Then convert it into a byte[]

ASM
byte[] mp3Byte = Mario.streamToByteArray(fileStream,1024)

);


here is the streamtobytearray method

C#
public static byte[] streamToByteArray(Stream stream, int initialLength)
    {
      // If we've been passed an unhelpful initial length, just
      // use 32K.
      if (initialLength < 1)
      {
        initialLength = 32768;
      }
      byte[] buffer = new byte[initialLength];
      int read = 0;
      int chunk;
      while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
      {
        read += chunk;
        // If we've reached the end of our buffer, check to see if there's
        // any more information
        if (read == buffer.Length)
        {
          int nextByte = stream.ReadByte();
          // End of stream? If so, we're done
          if (nextByte == -1)
          {
            return buffer;
          }
          // Nope. Resize the buffer, put in the byte we've just
          // read, and continue
          byte[] newBuffer = new byte[buffer.Length * 2];
          Array.Copy(buffer, newBuffer, buffer.Length);
          newBuffer[read] = (byte)nextByte;
          buffer = newBuffer;
          read++;
        }
      }
      // Buffer is now too big. Shrink it.
      byte[] ret = new byte[read];
      Array.Copy(buffer, ret, read);
      return ret;
    }


Then send it to the db as a field
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900