9 Replies - 8038 Views - Last Post: 23 January 2012 - 02:43 PM Rate Topic: -----

#1 alexis04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-January 12

any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 07:11 PM

plss help how can i convert or how can i put images in my database using sql server 2005 and vb.net2010
Is This A Good Question/Topic? 0
  • +

Replies To: any could give me codes for puting pictures in sql server 2005

#2 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 07:23 PM

Convert the image to a byte array and insert it into a record in the database, then when you want to use it, retrieve the byte array from the database and convert it back.

Image To Byte Array

Convert the byte array to an image by using a memory stream, and the image.fromstream function. There are many ways to do this. It just depends on how dirty you want to get. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 alexis04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-January 12

Re: any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 09:14 PM

tnx sir i stored it already to database but i want now to view it using a picture box how can i convert it again to image format?

pic1 = ds.Tables("tblStudents").Rows(inc).Item("Photo")

this is the code but its not working
Was This Post Helpful? 0
  • +
  • -

#4 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 10:31 PM

View Postalexis04, on 22 January 2012 - 10:14 PM, said:

tnx sir i stored it already to database but i want now to view it using a picture box how can i convert it again to image format?

pic1 = ds.Tables("tblStudents").Rows(inc).Item("Photo")

this is the code but its not working


First make sure you are retrieving the right record.

Next create a memory stream.

Declare a temporary image
dim tempimage as image



Feed the byte array of the image to the memory stream. Then feed the memorystream to the image.fromstream function. Your byte array should go into a byte array, byte() or the variable() as byte or as byte() many variations etc.

Here is a reference.

This will return an image from the passed byte array argument.
Was This Post Helpful? 0
  • +
  • -

#5 alexis04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-January 12

Re: any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 10:42 PM

Imports System
Imports System.Data
Imports System.Data.SqlClient


Public Class Student
    Dim inc As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim OpenFileDialog1 As New OpenFileDialog
        With OpenFileDialog1
            .CheckFileExists = True
            .ShowReadOnly = False
            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
            .FilterIndex = 2
            If .ShowDialog = DialogResult.OK Then
                ' Load the specified file into a PictureBox control.
                pic1.Image = Image.FromFile(.FileName)
            End If
        End With

    End Sub


    Private Sub Student_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = ConnectDB()
        Try
            con.Open()
            da = New SqlDataAdapter("select first_name,middle_name,last_name,year_level,section,sex,Photo from tblStudents", con)
            da.Fill(ds, "tblstudents")
            con.Close()
        Catch ex As Exception
            MsgBox("Error")
            MsgBox(ex.ToString)
            con.Close()
        End Try
        DataGridView1.DataSource = ds.Tables("tblStudents")
        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        txtFirstName.Text = ds.Tables("tblStudents").Rows(inc).Item("First_Name")



    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub
End Class



heres my code sir i dont know where to put them could u edit it and ill try to debug it
Was This Post Helpful? 0
  • +
  • -

#6 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: any could give me codes for puting pictures in sql server 2005

Posted 22 January 2012 - 10:53 PM

Where is pic1 declared? What is it declared as? You need to select the information from your dataset and store it as an array of bytes. Retrieve it and if it's of the proper type feed that as a constructor to your memory stream. the reference I gave you is a good example. If you can't follow along that I suggest pursuing something different for a tad bit as it seems your understanding is quite shallow, and this is going to be more of a headache then.

If I were you the first thing I would do is make sure I'm getting the right information from the record in the dataset. So create a message box, and display it let's make sure our kinects aren't legos if ya get my meaning.

Here's how to properly convert an image file to bytes.

Convert Image File To Bytes
Was This Post Helpful? 0
  • +
  • -

#7 alexis04   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-January 12

Re: any could give me codes for puting pictures in sql server 2005

Posted 23 January 2012 - 05:58 AM

sir here i convert it now into bytes here the codes

Public Function ConvertImage(ByVal myImage As Image) As Byte()
        Dim mstream As New MemoryStream()
        myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)

        Dim mBytes(mstream.Length - 1) As Byte
        mstream.Position = 0
        mstream.Read(mBytes, 0, mstream.Length)
        Return mBytes

    End Function


but how can i insert it to sql what query command i should put??
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        con = ConnectDB()

       

        Try
            con.Open()
            mycommand = New SqlCommand("insert into tblStudent(photo) values( ConvertImage(Me.pic1.Image)' )", con)
            mycommand.ExecuteNonQuery()
            MessageBox.Show("Succesfully Added")
            Me.Close()


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            con.Close()

        End Try
    End Sub


plss help
Was This Post Helpful? 0
  • +
  • -

#8 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: any could give me codes for puting pictures in sql server 2005

Posted 23 January 2012 - 02:29 PM

Your query command is sql, but your variables are dynamic so you'll need to insert those variables properly.

An example of doing this properly would be this:
 Dim SQLStatement As String = "INSERT INTO Music(Artist, Song) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"



You have to format it properly, and kind of cut around it like I did here with a sample statement. Make sure you are inserting information that appeals to your columns data type, as well as make sure your column is accepting the right data type you need it to be as well!
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14768
  • View blog
  • Posts: 59,038
  • Joined: 12-June 08

Re: any could give me codes for puting pictures in sql server 2005

Posted 23 January 2012 - 02:35 PM

I'm always a big fan of converting an image to a byte array and storing that in the database...

http://www.dreaminco...snippet6253.htm
Was This Post Helpful? 0
  • +
  • -

#10 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: any could give me codes for puting pictures in sql server 2005

Posted 23 January 2012 - 02:43 PM

View Postmodi123_1, on 23 January 2012 - 03:35 PM, said:

I'm always a big fan of converting an image to a byte array and storing that in the database...

http://www.dreaminco...snippet6253.htm


Personally I think there are better methods.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1