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?
 
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

In the database, what datatype do I set the column as?  image or binary?  I'm thinking binary!?
0
 
SweetsGreenCommented:
image
0
 
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
0
 
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?
0
 
narmi2Author Commented:
sorry i mean 23,040 bytes and 15 bytes
0
 
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.
0
 
narmi2Author Commented:
IT'S ALIVE !!!

THANKS :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.