Click here to Skip to main content
15,884,099 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 86.3K   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

 
GeneralStream through IIS Pin
ABalta2-Nov-09 9:05
ABalta2-Nov-09 9:05 

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.