Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 95,475 VB.NET Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 975 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert

Register to Make This Box Go Away!


Insert image

 
Reply to this topicStart new topic

Insert image

madz_meg16
post 6 Jul, 2007 - 05:07 AM
Post #1


New D.I.C Head

*
Joined: 4 Jul, 2007
Posts: 2


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 27 Jul, 2007 - 08:26 PM
Post #2


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,329



Thanked 19 times

Dream Kudos: 7050

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


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.

CODE

'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.


--------------------
IPB Image
Facebook | My Blog | My Site | Forums | Rules | Twitter
Syntax_Terror => Friend and fellow code-a-holic. May you rest in piece my friend
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/5/08 03:49AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->