Problems with DB Image field upload

Z

Zw0n1m1r

Hallo guyz! :)

I've some problems when I try to perform a image upload into a SQL
Server 2000 DB Image field ...

I've tried both with the ADO.NET method and the ADODB method:

------------------------ ADO.NET ------------------------
Dim cnn As New SqlConnection(strConnection)
Dim workParam As New SqlParameter("@IPicture", SqlDbType.Image)

'I put in a bytes array the uploaded file
Dim bufferLen As Integer =
Convert.ToInt32(uploadedFile.PostedFile.InputStream.Length)
Dim buffer(bufferLen) As Byte
uploadedFile.PostedFile.InputStream.Read(buffer, 0, bufferLen)

Try
cnn.Open()
workParam.Direction = ParameterDirection.Input
workParam.Size = bufferLen
workParam.Value = buffer

Dim sqlCommandInsUpd As New SqlCommand("SP_INSERT", cnn)
sqlCommandInsUpd.CommandType = CommandType.StoredProcedure
sqlCommandInsUpd.Parameters.Add(workParam)
sqlCommandInsUpd.ExecuteNonQuery()

Catch exErr As System.Exception
Finally
cnn.Close()
cnn = Nothing
End Try
----------------------------------------------------------------
.... with this method I don't have errors but the bytestream writed in
the Db field is lightly different, so when I read the field with a
SELECT construct and I write out the image with the "binarywrite"
function the image appears corrupted ...

------------------------- ADODB --------------------------
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim parm As ADODB.Parameter

Dim bufferLen As Integer =
Convert.ToInt32(uploadedFile.PostedFile.InputStream.Length)
Dim buffer(bufferLen) As Byte
uploadedFile.PostedFile.InputStream.Read(buffer, 0, bufferLen)

cnn.Open(strConnection)

cmd.ActiveConnection = cnn
cmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
cmd.CommandText = "SP_INSERT"

parm = New ADODB.Parameter
parm.Name = "@IPicture"
parm.Type = ADODB.DataTypeEnum.adLongVarBinary
parm.Value = buffer
cmd.Parameters.Append(parm)

cmd.Execute()

parm = Nothing
cmd = Nothing
cnn.Close()
cnn = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top