How to store mp3 files in database and play the same

Posted by in ADO.NET category on for Beginner level | Points: 250 | Views : 31987 red flag

In this article we will learn how to store mp3 files in database and play the same by the help of media player dll file.


In this article we are going to store mp3 files in bytes format and access the same file to play using the window media player.


1. How to use the COM object.
2. To store mp3 files in bytes format in database.
3. To access and play the same mp3 file.

Using the code

1. Open Visual Studio 2010 -> Create New Project
2. Add a Window Form Application and drag and drop 1 tab control, 4 button controls, three textbox, 1 openfiledialog control, and three label controls.
3. Create two tab one to select and store mp3 files in database, and second to play the mp3 files. In the first tab, it will allow the users to choose mp3 files to upload and in the second tab it will provide option to users to play the mp3 file.
4. Once the user choose the mp3 file and click on the save button the file will be stored in the database which is given in the first tab. In the second tab user need to provide the song code to play the file.
5. Before writing this code we must need to add wmp.dll to our application which is stored in windows/system32 folder.

// Table will be like this in Microsoft Sql Server 2005
create table audio ( song image, id int primary key)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Media;

namespace Storing_and_playing_song_file_in_database
    public partial class Form1 : Form
        public Form1()

        private void btnbrowse_Click(object sender, EventArgs e)
             // Opens the mp3 files
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Text = openFileDialog1.FileName;


        private void btnsave_Click(object sender, EventArgs e)
                if (File.Exists(textBox1.Text)) // checks the file
                    FileStream file = new FileStream(textBox1.Text, FileMode.Open);
                    byte[] songdata = new byte[file.Length];

                    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mydatabase;User ID=sa;Password=password");
		    // Executing the command to insert song
                    SqlCommand cmd = new SqlCommand("insert audio values(@song," + Convert.ToInt32(textBox2.Text) + ")", con);
                    cmd.Parameters.AddWithValue("@song", songdata);
                    MessageBox.Show("Inserted Successfully.");
                    MessageBox.Show("Please choose the file first.");
            catch (Exception e1)
// Creating the object of window media player
       WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
       int count = 0;
       private void btnplaypause_Click(object sender, EventArgs e)
            if (count == 0)
                   SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mydatabase;User ID=sa;Password=password");
con.Open(); SqlCommand cmd = new SqlCommand("select song from audio where id=" + Convert.ToInt32(textBox3.Text) + "", con); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows) { File.WriteAllBytes("c:\\song.mp3", dr[0] as byte[]); player.URL = "c:\\song.mp3";; count = 1; btnplaypause.Text = "Pause"; con.Close(); player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange); } } catch (Exception e1) { MessageBox.Show(e1.Message); } } else if (player.playState==WMPLib.WMPPlayState.wmppsPaused) {; btnplaypause.Text = "Pause"; } else { player.controls.pause(); btnplaypause.Text = "Play"; } } private void Form1_Load(object sender, EventArgs e) { openFileDialog1.Filter = "*.mp3|*.mp3"; } private void Player_PlayStateChange(int newstate) { if (player.playState == WMPLib.WMPPlayState.wmppsMediaEnded) { btnstop_Click(null, null); } } private void btnstop_Click(object sender, EventArgs e) { player.controls.stop(); btnplaypause.Text = "Play"; count = 0; } } }
Screen 1: To store song

Screen 2: To play song


I hope this will be useful who wants to store mp3 files to database.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Imteyaz Ahmed
Member Level: Starter
Member Status: Member
Member Since: 4/25/2013 1:16:00 PM
Country: India
Currently working as a student and always try to innovate something new.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)