Blob: INSERT a file in SQL Server

M

Michael Maes

Hello,

I'm trying to INSERT all kind of files in a SQL Server 2000 Table.
These files are e-mail attachments (.PDF, .XLS, .DOC, ....).

Using the code below, returns an IConvertible error.
The DataType of the field is 'Text'. Should this be 'Image'?

What am I missing?

Michael

Dim sqlCONN As New SqlConnection(global.sdc)

Dim scmd As New SqlCommand("UPDATE tblEmailAttachments SET
attachment=@attachment WHERE emailID = " & emailID & " AND fileName = " &
fileName, sqlCONN)

Dim fs As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open,
IO.FileAccess.Read)

Dim b(fs.Length() - 1) As Byte

fs.Read(b, 0, b.Length)

fs.Close()

Dim P As New SqlParameter("@attachment", SqlDbType.Text, b.Length,
ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, b)

scmd.Parameters.Add(P)

sqlCONN.Open()

scmd.ExecuteNonQuery()

sqlCONN.Close()
 
S

Steven Bras [MS]

One of these two articles should get you going:

317034 HOW TO: Read and Write a File to and from a BLOB Column by Using
http://support.microsoft.com/?id=317034

and/or

317034 HOW TO: Read and Write a File to and from a BLOB Column by Using
http://support.microsoft.com/?id=317034

Hope this helps!

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 

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

Top