How to store and retrieve images from VB.NET and Sql Server 2008

No body has any idea about pictures in vb.net

No body has any idea about pictures in vb.net

Images in vb.net, Yes.
Images in Sql Server, No.

i want to load image in picturebox then store it in sql server 2008

Load image in picturebox:

Dim myCoolImage As String = "C:\vs logo.png"
        If IO.File.Exists(myCoolImage) Then
            PictureBox1.Image = Image.FromFile(myCoolImage)
        End If

Store it in sql server 2008:
???

Dear It is specifically for one image how can i load multiple image in picture box.

Do you mean you want to save image in the DB ?
If so then you have to make pictures folder in the app folder
And save the location in the DB

After you make the load dialog or any thing to put the image in the picbox,
The saving code :

PictureBox1.Image.Save(Environment.CurrentDirectory & "\images\" & nametextbox.text)
Dim Conn As SqlClient.SqlConnection
Dim Cmd As SqlClient.SqlCommand
Dim DR As SqlClient.SqlDataReader
Dim ConnStr As String = "PUT YOU CONNECTION STRING"
Dim SQL As String = "Insert into TABLENAME(COLUMNNAME) Values("@1")"
SQL = SQL.Replace("@1" , Environment.CurrentDirectory & "\images\" & nametextbox.text)
Conn = New SqlClient.SqlConnection(ConnStr)
Conn.Open()
Cmd = New SqlClient.SqlCommand(SQL, Conn)
Cmd.ExecuteNonQuery
Msgbox("Saved!")

And your loading code :

Dim Conn As SqlClient.SqlConnection
Dim Cmd As SqlClient.SqlCommand
Dim DR As SqlClient.SqlDataReader
Dim ConnStr As String = "PUT YOU CONNECTION STRING"
Dim SQL As String = "Select COLUMNNAME from TABLENAME"
Conn = New SqlClient.SqlConnection(ConnStr)
Conn.Open()
Cmd = New SqlClient.SqlCommand(SQL, Conn)
PictureBox1.ImageLocation = Cmd.ExcuteScalar.ToString()

I hope that help
Note: The pic name have to be like : pic1.jpeg, or any file extension

Dear It is specifically for one image how can i load multiple image in picture box.

Load multiple image in Picturebox?
Do you intend to load different images or to load multiple images as one image in a picturebox?

If for different images, use a listbox/listview to display a list of the images and since you now know how to load a single image, use that code for each item clicked in your listbox/listview.

To load multiple images as one image in one picturebox can be done.
The code is a little too advanced to understand, especially if having problems loading a single image.

By using this code we are inserting image into a folder but not in database. How can we save picture in Sql Server Database.

PictureBox1.Image.Save(Environment.CurrentDirectory & "\images\" & nametextbox.text)Dim Conn As SqlClient.SqlConnectionDim Cmd As SqlClient.SqlCommandDim DR As SqlClient.SqlDataReaderDim ConnStr As String = "PUT YOU CONNECTION STRING"Dim SQL As String = "Insert into TABLENAME(COLUMNNAME) Values("@1")"SQL = SQL.Replace("@1" , Environment.CurrentDirectory & "\images\" & nametextbox.text)Conn = New SqlClient.SqlConnection(ConnStr)Conn.Open()Cmd = New SqlClient.SqlCommand(SQL, Conn)Cmd.ExecuteNonQueryMsgbox("Saved!")PictureBox1.Image.Save(Environment.CurrentDirectory & "\images\" & nametextbox.text)
Dim Conn As SqlClient.SqlConnection
Dim Cmd As SqlClient.SqlCommand
Dim DR As SqlClient.SqlDataReader
Dim ConnStr As String = "PUT YOU CONNECTION STRING"
Dim SQL As String = "Insert into TABLENAME(COLUMNNAME) Values("@1")"
SQL = SQL.Replace("@1" , Environment.CurrentDirectory & "\images\" & nametextbox.text)
Conn = New SqlClient.SqlConnection(ConnStr)
Conn.Open()
Cmd = New SqlClient.SqlCommand(SQL, Conn)
Cmd.ExecuteNonQuery
Msgbox("Saved!")

No body has any idea about images.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.