#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Apr 2005
    Posts
    2
    Rep Power
    0

    Red face Insert images in sqlserver 2000 using asp.net


    Hi All,

    Please explain how can i insert the images in sqlserver 2000 using Asp.net page.

    And also how can i insert the images in table using simple query.
    Thanks
  2. #2
  3. Senior Water Wizard
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2004
    Location
    Cape Town, RSA
    Posts
    10,233
    Rep Power
    862
    Hi,

    first of all, its not a good idea to insert images directly into the database, most of the users here would suggest, and I, to only store the url in the database and upload the image to a folder rather than sticking them into the database.

    hope this helps
    Look! Its a ShemZilla


  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Apr 2005
    Location
    Hyderabad
    Posts
    2
    Rep Power
    0

    Arrow Inserting Images to SqlServer in ASP .NET


    hi
    check the below code:
    ----------------------

    Inserting Images to SqlServer in ASP .NET
    Introduction
    There will be many occassion, in which we will be urged to store images in the Database. In some applications we may have some sensitive information which cannot be stored in a file system, since if anything is in the file system, then it may be very easy for the users to hack the pictures/images.

    In this article, we will discuss about, how we can insert images to a SqlServer 2000.

    We will be learning the following aspects in this article.
    Prerequistes for inserting an image file
    Working with the Stream Object
    Finding the Size and Type of the image that is going to be uploaded
    How to use the InputStream method?

    Prerequistes for inserting an image file
    Two primary things that we need before the upload begins are

    # The property enctype of the Form tag should be set to enctype="multipart/form-data"
    # We should have a <input type=file> which allows the user to select the necessary image file (which will be inserted into the database)
    # Also we need to Import the Namespace, System.IO to deal with the Stream object.

    The above three points applies to an ASPX page. Also we need to have the following prerequistes in the SqlServer.


    Code in the OnClick event of the Submit button.


    Dim intImageSize As Int64
    Dim strImageType As String
    Dim ImageStream As Stream

    ' Gets the Size of the Image
    intImageSize = PersonImage.PostedFile.ContentLength

    ' Gets the Image Type
    strImageType = PersonImage.PostedFile.ContentType

    ' Reads the Image
    ImageStream = PersonImage.PostedFile.InputStream

    Dim ImageContent(intImageSize) As Byte
    Dim intStatus As Integer
    intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

    ' Create Instance of Connection and Command Object
    Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
    Dim myCommand As New SqlCommand("sp_person_isp", myConnection)

    ' Mark the Command as a SPROC
    myCommand.CommandType = CommandType.StoredProcedure

    ' Add Parameters to SPROC
    Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)
    prmPersonImage.Value = ImageContent
    myCommand.Parameters.Add(prmPersonImage)

    Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)
    prmPersonImageType.Value = strImageType
    myCommand.Parameters.Add(prmPersonImageType)

    Try
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myConnection.Close()
    Response.Write("New person successfully added!")
    Catch SQLexc As SqlException
    Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
    End Try
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Apr 2005
    Posts
    2
    Rep Power
    0
    hi,

    Thanks for your reply and the code is very helful.

Similar Threads

  1. Uploading images in ASP.NET Community Starter Kit
    By aquafoil in forum .NET Development
    Replies: 1
    Last Post: March 8th, 2005, 11:25 PM
  2. Replies: 4
    Last Post: November 18th, 2004, 11:58 AM
  3. Windows 2003 Hosting: ASP.net + MSSQL 2000 (standard)
    By amzhost in forum Windows Web Hosting
    Replies: 0
    Last Post: July 16th, 2004, 02:43 PM
  4. ASP.NET: error insert on access DB
    By jdaems in forum .NET Development
    Replies: 1
    Last Post: April 26th, 2004, 06:35 PM
  5. Using ASP.NET in Visual Studio.NET
    By Red in forum ASP Development
    Replies: 6
    Last Post: April 7th, 2004, 03:30 PM

IMN logo majestic logo threadwatch logo seochat tools logo