inserting a zip file into a varbinary column in a database

  • anyone know how to write an insert statement that will allow you to write a zip file into a table column designated as type varbinary? I would like to write and insert state and a retrieve statement.

    Here is my attempt at writing a zip file:

    Table: mps _data

    columns:

    mps_id long

    mps_session_id varchar(128)

    mps_request varbinary

    local variables

    ll_mps_id long value 1

    ls_session string value 'userid' + datetime

    ls_zipfile string location of zipfile (ie c:\temp\zipfile.zip)

    insert into mps_data (mps_id, mps_session_id, mps_request) values ( :ll_mps_id, :ls_session, CONVERT(varbinary(max), :ls_zipfile));

    writes a binary value that is illegible. When I try to retrieve it creates a blank zip file that cannot be opened. Has anyone else been able to write a simple insert and retrieval process for zip files?

  • usually this is done in a programming language, as there's a lot more tools and control there.

    Doing it in TSQL only, the file has to exist, and you have to do it one file at a time, and the file has to be on the server or in a UNC (\\Server\Share\Filename) path the service account that SQL server is using has access to(doesn't matter if YOU have access to it):

    INSERT INTO img_item(img_data)

    SELECT * FROM OPENROWSET(BULK N'C:\myfile.zip', SINGLE_BLOB) AS img_data

    In a .NET application a binary file can be loaded into a byte array, then inserted.

    ' VB.NET example

    ' Load file into a byte array

    Dim fi As New System.IO.FileInfo("c:\myfile.zip")

    Dim fs As System.IO.FileStream = fi.OpenRead

    Dim lBytes As Long = fs.Length

    Dim myImage(lBytes) As Byte

    fs.Read(myImage, 0, lBytes)

    fs.Close()

    ' Insert binary data into database

    Using myConn as New Data.SqlClient.SqlConnection(My.Settings.myConnectionString)

    Dim myCommand as new Data.SqlClient.SqlCommand("INSERT INTO img_item VALUES (@img_data)", myConn)

    myCommand.Parameters.AddWithValue("@img_data",myImage)

    myConn.open()

    myCommand.ExecuteNonQuery()

    myConn.Close()

    End Using

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Sorry, guess I should have prefaced it by saying I am writing the query in Powerbuilder 12.5. I am retrieving a file to write to the database for batch processing to a webservice and the zip file would allow us to control table size from growing exponentially. the zip file can contain anywhere from 1 to 50,000 rows of data. I am trying to write an inline insert and then write an inline retrieve of te zip file to then unzip the results for viewing results of the webservice response file. Visual Basic is a little different, but I will check to see if I can adapt to my efforts. Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply