SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


inserting a zip file into a varbinary column in a database


inserting a zip file into a varbinary column in a database

Author
Message
kmlevee
kmlevee
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 6
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?
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28445 Visits: 39963
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!

kmlevee
kmlevee
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 6
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search