Home


.Net, SQL and softball. Musings of a software developer with a softball problem

Tuesday, January 8, 2008

Uploading a file into an Oracle Blob

I was actually pleasantly surprised how easy it was to upload a blob into an Oracle database from a .NET app. I had been putting off this part of the app off for quite some time, and I am just about done with the rest of it so I thought it was about time. And I'll share the code here with you:

I just used a standard ASP.Net file upload control in the front end (not included here), and the code behind was just as simple


'file upload control
fileName = Me.fupAttach.FileName

'friendly name for the file
friendlyName = Me.txtFriendlyName.Text

'quick description
FileDesc = Me.txtFileDesc.Text

'Since a blob is just a byte array, the file upload control has a nice built in
'option to convert the file into a byte[]
Dim btAry As Byte() = Me.fupAttach.FileBytes

'The rest is just basic Oracle

Dim conn As New OracleConnection
Dim comm As OracleCommand

conn.ConnectionString = .Common.ProjectConnectionString
conn.Open()

comm = New OracleCommand("############", conn)
comm.CommandType = CommandType.StoredProcedure

comm.Parameters.Add("IN_FILE_NAME", OracleType.VarChar, 500).Value = fileName
comm.Parameters.Add("IN_FRIENDLY_NAME", OracleType.VarChar, 1).Value = friendlyName
comm.Parameters.Add("IN_FILE_DESC", OracleType.VarChar, 220).Value = FileDesc
comm.Parameters.Add("IN_FILE_BLOB", OracleType.Blob).Value = btAry

comm.ExecuteNonQuery()


And that is it. Like I said pretty easy. I was actually very surprised how little I found on the internet to do this. Most of the other examples I found were using a standard HTML upload filed, back from classic ASP days.

Hope this helps.

Labels: ,

posted by Tom Becker at

0 Comments:

Post a Comment

<< Home

Powered by Blogger

Subscribe to
Posts [Atom]

Name: Tom Becker
Location: Richmond, VA, United States