Save MP3 Files In Database

Hi

I am working on a little program which should "hopefully" let the user upload an mp3 file from their harddrive to a database for storage.

Anyone know how to do this?

Thanks
LVL 1
narmi2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramesh SrinivasTechnical ConsultantCommented:
Hi,

You would do this in the same way as images - i.e convert the files data into binary.

You would then store it in the database.

Examples of how to achieve this:

http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1
http://www.eggheadcafe.com/articles/20041022.asp


regards,

KS

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

oleggoldCommented:
This seems exactly what You need:
http://www.freevbcode.com/ShowCode.asp?ID=1127
narmi2Author Commented:
Thanks for the replies.

In the database, what datatype do I set the column as?  image or binary?  I'm thinking binary!?
SweetsGreenCommented:
image
narmi2Author Commented:
Thanks!

Ok I now have the following code which does NOT work properly.  I upload a 23k file and it returns a 1k file ???

THE CODE -----------------


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        WriteDocumentToDB2("c:\test.doc")
    End Sub

    Private Sub WriteDocumentToDB2(ByVal sInDoc As String)
        Dim objDataSet As DataSet
        Dim objSQLCommand As SqlCommand
        Dim objSQLConnection As New SqlConnection("connection string")
        Dim strInsertCommand As String = "insert into table (name, phone, docs) values (@name, @phone, @docs)"

        Dim objFileStream As New FileStream(sInDoc, FileMode.OpenOrCreate, FileAccess.Read)
        Dim objDocBytes(objFileStream.Length) As Byte

        objFileStream.Read(objDocBytes, 0, objFileStream.Length)
        objFileStream.Close()

        objSQLCommand = New SqlCommand(strInsertCommand, objSQLConnection)

        objSQLCommand.Parameters.Add(New SqlParameter("@name", SqlDbType.NVarChar, 50))
        objSQLCommand.Parameters("@name").Value = "Some Value Here"

        objSQLCommand.Parameters.Add(New SqlParameter("@phone", SqlDbType.NVarChar, 50))
        objSQLCommand.Parameters("@phone").Value = "Some Value Here"

        objSQLCommand.Parameters.Add(New SqlParameter("@docs", SqlDbType.Image, 16))
        objSQLCommand.Parameters("@docs").Value = objDocBytes

        objSQLCommand.Connection.Open()
        objSQLCommand.ExecuteNonQuery()
        objSQLCommand.Connection.Close()
    End Sub

    Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
        ReadDocumentFromDB2("c:\a\test.doc")
    End Sub


    Public Sub ReadDocumentFromDB2(ByVal sOutDoc As String)
        Dim objDataSet As DataSet
        Dim objSQLConnection As New SqlConnection("connection string")
        Dim objSQLDataAdapter As SqlDataAdapter

        Dim objSelectCommand As String = "select docs from table1 where id = @id"

        objSQLDataAdapter = New SqlDataAdapter(objSelectCommand, objSQLConnection)

        objSQLDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@id", SqlDbType.Int, 4))
        objSQLDataAdapter.SelectCommand.Parameters("@id").Value = "0"

        objDataSet = New DataSet
        objSQLDataAdapter.Fill(objDataSet)

        Dim objDataRow As DataRow
        objDataRow = objDataSet.Tables(0).Rows(0)

        Dim objDocBytes() As Byte
        objDocBytes = objDataRow("docs")

        Dim K As Long
        K = UBound(objDocBytes)

        Dim objFileStream As New FileStream(sOutDoc, FileMode.OpenOrCreate, FileAccess.Write)
        objFileStream.Write(objDocBytes, 0, K)
        objFileStream.Close()
        objFileStream = Nothing
    End Sub
narmi2Author Commented:
To be more precise it uploads 23.040 bytes and returns 15 bytes!  That 15 bytes is very close to the 16 byte image datatype: i.e. objSQLCommand.Parameters.Add(New SqlParameter("@docs", SqlDbType.Image, 16))

Any ideas?
narmi2Author Commented:
sorry i mean 23,040 bytes and 15 bytes
narmi2Author Commented:
If I could offer more points I would, as this is very urgent.  anyone know from the code posted above, why I only get a 15 byte file when i upload a 23,050 byte file ???

Please help.
narmi2Author Commented:
IT'S ALIVE !!!

THANKS :D
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.