1 Replies - 30260 Views - Last Post: 27 July 2007 - 08:26 PM Rate Topic: -----

#1 madz_meg16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-July 07

Insert image

Post icon  Posted 06 July 2007 - 05:07 AM

I have a question about inserting an image.
I want to insert an image from vb.net to a database and i don't know what is the right syntax for it
Is This A Good Question/Topic? 0
  • +

Replies To: Insert image

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Insert image

Posted 27 July 2007 - 08:26 PM

To insert an image into a SQL Server first make sure the data type of the column the image is being added to is the Image data type. So for the code to save the image into the database (This is code from a web stand point, some minor changes will have to be made if this is being used in a windows application.

'Declare the variables you need
Dim iImageSize As Int64
Dim sImageType As String
Dim ImageStream As Stream

' Gets the Size of the Image
iImageSize = File1.PostedFile.ContentLength

' Gets the Image Type
sImageType = File1.PostedFile.ContentType

' Reads the Image
ImageStream = File1.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 cnSqlCon As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim cmdSqlCommand As New SqlCommand("YourStoredProcedureName", cnSqlCon)

' Mark the Command as a stored procedure
cmdSqlCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim prmPersonImage As New SqlParameter("@MyImage", SqlDbType.Image)
prmPersonImage.Value = ImageContent
myCommand.Parameters.Add(prmPersonImage) 
Try
	cnSqlCon.Open()
	cmdSqlCommand.ExecuteNonQuery()
	cnSqlCon.Close()
	Response.Write("New person successfully added!")
Catch SQLexc As SqlException
	Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
End Try



This should at least get you started down the right path.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1