Click here to Skip to main content
13,556,447 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 19 Sep 2008
Licenced GPL3

Store audio in Sql Server 2005 and retrieve it and play it in windows application .net.

, 19 Sep 2008
Rate this:
Please Sign up or sign in to vote.
this article demonstrate how to insert a audio file into SQL Server and how to get the audio file from SQL Server and play it in .Net


Hi guys,in this article you are presented with how to Store/retrieve audio files in SQL Server 2005.the main aspect of this article is how to store the audio data which is in binary format into SQL Server.The Process of Retrieving this Data again from Sql Server and playing it using Windows media player component object model tool is some how tricky!

As you know all of audio files and pictures in your computer are in Binary format.that mean internally they have 0's and 1's.So if you want to store these kind of files into SQL Server 2005,you should have a field in SQL Server 2005 which data type should be "varbinary(max)".

this data type is capable of storing binary files in it.note that you can store anything in this kind of data type,there is no restriction if you wish to store some text files in this field(varbinary), because every Data in computer at its last point is converted to 0 and 1 (binary).


You should be familiar with C# and Sql Server and File processing in .Net.if So then hop to next stage.

Using the Code

In my code i have used a database named "Sample".There is a table (tblVoice) in it which has a field (fldVoice)in binary format.I mean its data type is varbinary(max).This is all about the Sql Server 2005 i used in my project.

I created a win application project in c# my form i have used two button one for inserting the audio file into SQL SErver 2005 and another for retrieving it from SQL sErver and playing that in Windows media player.There is also a windows media player in my form.i brought that from going to toolbox and right clicking and choosing an item in com this tab you should navigate to windows media player and afrer finding that clicking ok to attach this COM object to your toolbox.

Then you should drag and drop this windows media player into your form.and the rest of the work is done in Click event of the buttons.

In my project i have an audio file in my D:\ drive by the name of m.wma.

For inserting an audio file you should first convert that file into arrays of this way you can then store this byte array into fldVoice.

    byte[] stream = File.ReadAllBytes(@"d:\m.wma");
com.Parameters.AddWithValue("@voice", stream);

For retrieving the audio file from the database you should simply cast the retrieved data into byte[] array:

   SqlCommand com = new SqlCommand("select * from tblVoice", con);
DataTable dt = new DataTable();
SqlDataReader dr = com.ExecuteReader();
byte[] stream = (byte[])dt.Rows[0][0];

Windows media player can read cant read any stream.because of this drawback i first created a file with .wma extension and store my byte[] array in it and after that i give this file to URL of windows media player for playing my file:

File.WriteAllBytes("D:\\news.wma", stream);

axWindowsMediaPlayer1.URL = "D:\\news.wma";

Points of Interest

I came to this concludion that windows media player is just able to read a file no Stream.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Morteza Naeiamabadi
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
independent IT Consultant,Currently engaged with Sharepoint developing and MVC apps

You may also be interested in...

Comments and Discussions

Questionhelp Pin
Member 123778558-Mar-16 9:38
memberMember 123778558-Mar-16 9:38 
Questionrequesting coding regarding Pin
Member 1138365627-Jan-15 6:01
memberMember 1138365627-Jan-15 6:01 
Suggestioncomment and question Pin
Member 1016455613-Jan-14 22:26
memberMember 1016455613-Jan-14 22:26 
GeneralMy vote of 4 Pin
Kanasz Robert25-Sep-12 22:37
mvpKanasz Robert25-Sep-12 22:37 
QuestionIs there another way to read the audio or video file ?? Pin
ayman_sharkawy10-Jul-12 11:27
memberayman_sharkawy10-Jul-12 11:27 
QuestionStoring of Audio in SQL SERVER 2005 and its retrieval using JAVA Application Pin
ShwetaSaxena22-Dec-11 18:47
memberShwetaSaxena22-Dec-11 18:47 
GeneralStream through IIS Pin
ABalta2-Nov-09 9:05
memberABalta2-Nov-09 9:05 
General[My vote of 2] well..few comments Pin
Md. Marufuzzaman28-Aug-09 8:16
groupMd. Marufuzzaman28-Aug-09 8:16 
GeneralThanks Pin
Mohammad Rastkar27-Aug-09 3:33
memberMohammad Rastkar27-Aug-09 3:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 20 Sep 2008
Article Copyright 2008 by Morteza Naeiamabadi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid