Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - storing images in an image column
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

storing images in an image column

 Post Reply Post Reply
Author
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Topic: storing images in an image column
    Posted: 14 December 2005 at 5:38am
Hi all,
 
I have a big problem that needs to be resolved.
 
I am using SQL Server and ASP.NET (C#) and I have to store the Images in the DB. I have the fields already and there are already images in the DB, but I did not write the code to insert the images and I don't have the source for it.
 
Does anyone know how this is done?
 
I know the field is a binary field, but it's getting the image in there is the problem.
 
Thanks in advance,
 
Lee


Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 14 December 2005 at 10:37am
Basically you convert it to byte() and just store it.
 

Dim length As Integer = CInt(FileUpload.PostedFile.InputStream.Length)
                
Dim contentType As String = FileUpload.PostedFile.ContentType
              
Dim content(length) As Byte
            
FileUpload.PostedFile.InputStream.Read(content, 0, length)
              
documents.UpdateDocument(moduleId, itemId, Context.User.Identity.Name, NameField.Text, PathField.Text, CategoryField.Text, content, length, contentType)
 
and then my upload class looks like this:
 

Public Sub UpdateDocument(ByVal moduleId As Integer, ByVal itemId As Integer, ByVal userName As String, ByVal name As String, ByVal url As String, ByVal category As String, ByVal content() As Byte, ByVal size As Integer, ByVal contentType As String)
 
            If userName.Length < 1 Then

                userName = "unknown"

            End If
 
            ' Create Instance of Connection and Command Object

            Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

            Dim myCommand As New SqlCommand("Portal_UpdateDocument", myConnection)

            myCommand.CommandType = CommandType.StoredProcedure
 
             Dim parameterItemID As New SqlParameter("@ItemID", SqlDbType.Int, 4)

            parameterItemID.Value = itemId

            myCommand.Parameters.Add(parameterItemID)
 
            Dim parameterModuleID As New SqlParameter("@ModuleID", SqlDbType.Int, 4)

            parameterModuleID.Value = moduleId

            myCommand.Parameters.Add(parameterModuleID)
 
            Dim parameterUserName As New SqlParameter("@UserName", SqlDbType.NVarChar, 100)

            parameterUserName.Value = userName

            myCommand.Parameters.Add(parameterUserName)
 
            Dim parameterName As New SqlParameter("@FileFriendlyName", SqlDbType.NVarChar, 150)

            parameterName.Value = name

            myCommand.Parameters.Add(parameterName)
            Dim parameterFileUrl As New SqlParameter("@FileNameUrl", SqlDbType.NVarChar, 250)

            parameterFileUrl.Value = url

            myCommand.Parameters.Add(parameterFileUrl)
 
            Dim parameterCategory As New SqlParameter("@Category", SqlDbType.NVarChar, 50)

            parameterCategory.Value = category

            myCommand.Parameters.Add(parameterCategory)
 
            Dim parameterContent As New SqlParameter("@Content", SqlDbType.Image)

            parameterContent.Value = content

            myCommand.Parameters.Add(parameterContent)
 
            Dim parameterContentType As New SqlParameter("@ContentType", SqlDbType.NVarChar, 50)

            parameterContentType.Value = contentType

            myCommand.Parameters.Add(parameterContentType)
 
            Dim parameterContentSize As New SqlParameter("@ContentSize", SqlDbType.Int, 4)

            parameterContentSize.Value = size

            myCommand.Parameters.Add(parameterContentSize)
 
            myConnection.Open()

            myCommand.ExecuteNonQuery()

            myConnection.Close()
 
        End Sub

 
 
 
hope this helps. Now you may be able to strip some parameters if you don't need them.
 
 
 
 


Edited by michael - 14 December 2005 at 10:40am
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 14 December 2005 at 10:41am
sory to stretch it out like this, but this RTE does not like pastes from VS.
 
Gruss
Michael
Back to Top
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Posted: 14 December 2005 at 11:08am
Thanks Michael.
 
It works a treat.
 
Lee


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.