Click here to Skip to main content
16,016,022 members
Articles / Database Development / SQL Server
Article

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

Rate me:
Please Sign up or sign in to vote.
3.77/5 (18 votes)
19 Sep 2008GPL33 min read 87.6K   12.9K   38   9
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
Image 1

Introduction

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).

Background

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# language.in 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 tab.in 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 byte.in this way you can then store this byte array into fldVoice.

C#
    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:

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

Windows media player can read files.it 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:

C#
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.

License

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


Written By
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
independent IT Consultant,Currently engaged with Sharepoint developing and MVC Asp.net apps

Comments and Discussions

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