Click here to Skip to main content
14,599,401 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can store them as BLOBs. However, you should consider storing the .mp3 files on the server and storing their paths in the database.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

First you need to load the file into a stream

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



Then convert it into a byte[]

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

);


here is the streamtobytearray method

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100