Upload to SQL and retrieve

Discussion in '.NET' started by tmeers, Oct 18, 2006.

  1. tmeers

    tmeers

    Joined:
    Oct 18, 2006
    Messages:
    1
    Likes Received:
    0
    Ok so I have a site uploading to an SQL DB via an asp.net form (see below).
    <<<
    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub uploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uploadButton.Click

    Dim filePath As String
    filePath = Me.FileInput.Value

    If Not File.Exists(filePath) Then
    Me.statusLabel.Text = "File path is invalid. Please try again."
    Return
    End If

    Dim fileType As String
    fileType = Path.GetExtension(filePath)

    Dim fileName As String
    fileName = Path.GetFileNameWithoutExtension(filePath)

    Dim fileLength As Integer
    fileLength = Me.FileInput.PostedFile.ContentLength

    Dim fileStream As Stream
    Dim buffer(fileLength) As Byte

    fileStream = Me.FileInput.PostedFile.InputStream
    fileStream.Read(buffer, 0, fileLength)

    Dim theConnection As SqlConnection
    theConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ImagesConnectionString").ConnectionString)

    Try

    Dim uploadSqlCommand As SqlCommand
    uploadSqlCommand = New SqlCommand("UploadImages", theConnection)

    uploadSqlCommand.CommandType = Data.CommandType.StoredProcedure

    uploadSqlCommand.Parameters.Add("@ImageName", SqlDbType.VarChar, 50)
    uploadSqlCommand.Parameters.Add("@Image", SqlDbType.Image)
    uploadSqlCommand.Parameters.Add("@ImageType", SqlDbType.VarChar, 5)
    uploadSqlCommand.Parameters.Add("@TimeDateAdded", SqlDbType.DateTime)

    uploadSqlCommand.Parameters(0).Value = fileName
    uploadSqlCommand.Parameters(1).Value = buffer
    uploadSqlCommand.Parameters(2).Value = fileType
    uploadSqlCommand.Parameters(3).Value = Date.Now

    theConnection.Open()

    uploadSqlCommand.ExecuteNonQuery()

    theConnection.Close()

    Me.statusLabel.Text = "File was successfully submitted to database"

    Catch ex As Exception
    Me.statusLabel.Text = "File was NOT successfully sbmitted to database. Exception was: " & ex.Message
    Finally
    If theConnection.State = ConnectionState.Open Then
    theConnection.Close()
    End If
    End Try

    End Sub
    End Class
    >>>
    And this page works perfect. But I need the other side of it and be able to pull that image or doc or what ever back out of the DB and use the content in other pages in my site. I am using a stored proc to add the image and have a stored proc to get the image just not the page yet. Maybe some one can lead me to an example of this out on the new or possibly have one that they made.
    The stored proc I have to get the image (still as a script):

    CREATE PROCEDURE GetImages
    AS
    SET NOCOUNT ON
    SELECT ImageName,Image,ImageType,ID as ImageIdentity
    FROM dbo.Images
    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    SET ANSI_NULLS ON
    GO

    I do think it will work. I cannot tell until I actually get the page to test it.
    By the way i'm running server 2003, .net 2.0, sqlexpress 2005 and creating my pages in VS2005.

    Any help would be great,
    Thanks to any one who can
    Tim
     
    tmeers, Oct 18, 2006
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.