Click here to Skip to main content
15,886,032 members
Articles / Database Development / SQL Server

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.4K   12.9K   38  
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=Sample;integrated security=true");
            SqlCommand com = new SqlCommand("insert into tblVoice(fldvoice) values(@voice)", con);
            byte[] stream = File.ReadAllBytes(@"d:\m.wma");
            if (stream.Length > 0)
            {
                com.Parameters.AddWithValue("@voice", stream);
                con.Open();
                int result=com.ExecuteNonQuery();
                if (result > 0)
                    MessageBox.Show("insert done");
                con.Close();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=Sample;integrated security=true");
            SqlCommand com = new SqlCommand("select * from tblVoice", con);
            con.Open();
            DataTable dt = new DataTable();
            SqlDataReader dr = com.ExecuteReader();
            dt.Load(dr);
            dr.Close();
            con.Close();
            byte[] stream = (byte[])dt.Rows[0][0];
            File.WriteAllBytes("D:\\news.wma", stream);

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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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