locked
Add PDF to MySQL BLOB field RRS feed

  • Question

  • User-1427396445 posted

    Hey,

    I have a pdf that is stored on the server and I want to be able to import it into a mysql database using asp.net 2.

    I have the database etc all ready to go with the field set up as a blob. The pdf file is only ~3kb.

    I've tried a couple of things, but I'm sort of stuck with the actual process of getting that file into the db - ie. i'm not sure if it needs to be opened first, then read into an inputstream, etc and what type the input parameter needs to be set to (eg. MIMEType, or String, etc).

    Can anybody help out with some code for the Parameters and method of getting that pdf into the db using SQL

    Thursday, December 11, 2008 8:33 PM

Answers

  • User1634106055 posted

    I got that - the part of the links I was referring to was the file-to-database portion...  Not the upload.  2 of the articles talked about saving to SQL and the last about Oracle...  Here's another shot...

    from http://support.microsoft.com/kb/326502...

     Dim con As New SqlConnection("Server=yileiw2;uid=sqlauth;pwd=sqlauth;database=pubs")
            Dim da As New SqlDataAdapter("Select * From pub_info", con)
            Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim ds As New DataSet()

            da.MissingSchemaAction = MissingSchemaAction.AddWithKey
            con.Open()
            da.Fill(ds, "Pub_info")

            Dim fs As New FileStream _
             ("C:\SomePath\MyPhoto.jpg", FileMode.OpenOrCreate, _
              FileAccess.Read)
            Dim MyData(fs.Length) As Byte
            fs.Read(MyData, 0, fs.Length)
            fs.Close()
            ds.Tables("Pub_info").Rows(0)("logo") = MyData
            da.Update(ds, "Pub_info")

            fs = Nothing
            MyCB = Nothing
            ds = Nothing
            da = Nothing

            con.Close()
            con = Nothing
            Response.Write("File saved to database")

    I realize you're not working with SQL but I would think the coding is similar...

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2008 8:29 AM

All replies

  • User-1427396445 posted

    Thanks for the info, but the articles you linked to are not what I asked for :)

    I am after a method of importing a file that already exists on the server into the database. It is not for uploading a file from a local computer on the server and reading the HttpPostedFile.

    Thanks anyway.

    Sunday, December 14, 2008 4:51 PM
  • User1634106055 posted

    I got that - the part of the links I was referring to was the file-to-database portion...  Not the upload.  2 of the articles talked about saving to SQL and the last about Oracle...  Here's another shot...

    from http://support.microsoft.com/kb/326502...

     Dim con As New SqlConnection("Server=yileiw2;uid=sqlauth;pwd=sqlauth;database=pubs")
            Dim da As New SqlDataAdapter("Select * From pub_info", con)
            Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim ds As New DataSet()

            da.MissingSchemaAction = MissingSchemaAction.AddWithKey
            con.Open()
            da.Fill(ds, "Pub_info")

            Dim fs As New FileStream _
             ("C:\SomePath\MyPhoto.jpg", FileMode.OpenOrCreate, _
              FileAccess.Read)
            Dim MyData(fs.Length) As Byte
            fs.Read(MyData, 0, fs.Length)
            fs.Close()
            ds.Tables("Pub_info").Rows(0)("logo") = MyData
            da.Update(ds, "Pub_info")

            fs = Nothing
            MyCB = Nothing
            ds = Nothing
            da = Nothing

            con.Close()
            con = Nothing
            Response.Write("File saved to database")

    I realize you're not working with SQL but I would think the coding is similar...

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2008 8:29 AM
  • User1634106055 posted

    (that's mysql, not Oracle...  Oops...)

    Monday, December 15, 2008 8:32 AM
  • User-1427396445 posted

    No worries, I'll give that a shot tomorrow and let you know, thanks heaps. It looks like it should work with a little tweaking for MySQL.

    [:D] 

    Monday, December 15, 2008 9:27 AM
  • User-1427396445 posted

    With a little tweaking, it's now working pretty much using the code provided above.

    Thank you!! [:D]

    Tuesday, December 16, 2008 2:54 AM