tags:

views:

84

answers:

3

how to read MP3 from Sql database. in sql i have stored the file as binary format. now i want to retrive the Mp3 file stored in the sql and show in my aspx page. how????

pls help...

A: 

In its simplest form this is how you would get the raw bytes, can't really show any more without knowing what you want it for...

private byte[] GetMp3Bytes(string connString)
{
   SqlConnection conn = null;
   SqlCommand cmd = null;
   SqlDataReader reader = null;

   using (conn = new SqlConnection(connString))
   {
      conn.Open();

      using (cmd = new SqlCommand("SELECT TOP 1 Mp3_File FROM MP3_Table", conn))
      using (reader = cmd.ExecuteReader())
      {
          reader.Read();
          return reader["Mp3_File"] as byte[];
      }
   }
}
Steve Danner
ya i as wat u did here in this code. i got the file read as byte[]. how to show this byte[] in aspx webpage to load the mp3 song and display it in webpage????
Ranjana
What do you mean by "display it", do you want to play it when the user loads the page, or do you want the user to download it?
Steve Danner
A: 

Don't store the MP3 in the database.

Put the MP3 file in a directory and store the path to it in the database instead.

Chris Almond
Well that totally depends on what he wants to do with it!
Tom Gullen
Let me elaborate, maybe he wants to search the binary fields for certain values, compare binary data, manipulate it etc. Or perhaps the mp3's are for members only, and storing them in a database is a way to ensure no one external has access to them. There are lots of legitimate reasons for storing it in a database.
Tom Gullen
+1  A: 

You'd probably want to use a Generic ASHX Handler that retrieves the binary data and streams it to the response stream with the correct content-type header ("audio/mpeg").

If you look at the article Displaying Images in ASP.NET Using HttpHandlers then you should see the basic principle. You just need to change the content-type output.

Dan Diplo