Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

inserting a zip file into a varbinary column in a database Expand / Collapse
Author
Message
Posted Thursday, June 13, 2013 11:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 17, 2013 6:26 AM
Points: 2, 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?
Post #1463230
Posted Thursday, June 13, 2013 12:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1463248
Posted Thursday, June 13, 2013 12:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 17, 2013 6:26 AM
Points: 2, 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!
Post #1463265
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse