BigJ, Here's a working sample.
1) In SQL Server define your image column (VARBINARY(MAX)). You may
want to create a separate table for the image and relate it via a FK
if the original table is one that is searched often for other values.

2) Create a stored procedure to update the image:
           CREATE PROCEDURE dbo.uspSampleInsert
           (
            @FK_ID int,
            @theImage varbinary(max)
           )
            AS
            SET NOCOUNT OFF;
            INSERT INTO dbo.imagetable(myFK, myimage) VALUES(@FK_ID,
@theImage);
    If you are using an "identity" key, you may want to return the new
image ID by selecting SCOPE_IDENTITY()

3) Create a class to handle the data access to the imagetable, which
will include an Insert function. I generally inherit from a base class
which includes code to get the connection string and create a database
object (I use Unity for that -- works great).

        Public Class ImageDA
            Inherits BaseDataAccessObject
        Public Sub New()
        End Sub
        Public Function Insert(ByVal ID As Integer, _
                               ByVal document As IO.Stream) As Boolean
_
                               Implements Interfaces.IEmpResume.Insert
            Dim sqlCommand As String = "dbo.uspSampleInsert"
            Dim dbCommand As System.Data.Common.DbCommand =
CreateProcCommand(sqlCommand)
            Try
                'Do not update if no data
                If IsEmptyString(filepath) Or document Is Nothing Then
                    'Dim rdr As SqlDataReader = Nothing
                    Return False
                End If

                Dim fLen As Integer = CInt(document.Length)
                Dim buffer(fLen) As Byte
                document.Read(buffer, 0, fLen)
                myDatabase.AddInParameter(dbCommand, "@FK_ID",
DbType.Int32, ID)
                myDatabase.AddInParameter(dbCommand, "@theImage",
DbType.Binary, buffer)

                myDatabase.ExecuteNonQuery(dbCommand)
                Return True

            Catch ex As Exception
                Return False
            Finally
                dbCommand.Dispose()
            End Try
        End Function
        End Class

4)  An easy way to get the data is to use a dialog to select and open
the file to upload.
    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
BindingNavigatorAddNewItem.Click
        Dim fdialog As New OpenFileDialog
        With fdialog
            .CheckFileExists = True
            .CheckPathExists = True
            .DefaultExt = ".jpg"
            .Filter = "JPEG files (*.jpg)|*.jpg|GIF files (*.gif)|
*.gif"
            .Multiselect = False
            .Title = "Select Image"
        End With
        If fdialog.ShowDialog() = System.Windows.Forms.DialogResult.OK
Then
            Dim da As New ImageDA
            Try
                If Not String.IsNullOrEmpty(fdialog.FileName) Then
                    da.Insert(CType(txtID.Text, Integer),
fdialog.OpenFile)
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original
error: " & Ex.Message)
            Finally
                fdialog.Dispose()
            End Try
        End If

    End Sub

On Nov 9, 9:37 pm, BigJ <[EMAIL PROTECTED]> wrote:
> How do you load images into a table?  I'm using Visual Web Developer,
> and there is a selectable datatype called image. I've selected the and
> created a directory in my root directory called 'img' that stores all
> the images.  How do I proceed to load the image into the table?
>
> Thanks

Reply via email to