Link to home
Start Free TrialLog in
Avatar of narmi2
narmi2

asked on

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
Avatar of Ramesh Srinivas
Ramesh Srinivas
Flag of United Kingdom of Great Britain and Northern Ireland image

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
ASKER CERTIFIED SOLUTION
Avatar of Naveen Swamy
Naveen Swamy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This seems exactly what You need:
http://www.freevbcode.com/ShowCode.asp?ID=1127
Avatar of narmi2
narmi2

ASKER

Thanks for the replies.

In the database, what datatype do I set the column as?  image or binary?  I'm thinking binary!?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of narmi2

ASKER

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
Avatar of narmi2

ASKER

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?
Avatar of narmi2

ASKER

sorry i mean 23,040 bytes and 15 bytes
Avatar of narmi2

ASKER

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.
Avatar of narmi2

ASKER

IT'S ALIVE !!!

THANKS :D