Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,287 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 976 people online right now. Registration is fast and FREE... Join Now!




Insert image

 
Reply to this topicStart new topic

Insert image

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

New D.I.C Head
*

Joined: 3 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
+Quote Post


PsychoCoder
RE: Insert Image
27 Jul, 2007 - 07:26 PM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,283



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:19PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month