Results 1 to 7 of 7

Thread: How do I Add photo records in sql through VB .net

  1. #1
    Join Date
    Feb 2007
    Posts
    2
    Rep Power
    0

    Default How do I Add photo records in sql through VB .net

    How do I Add photo records in sql through VB .net

  2. #2
    Join Date
    Dec 2004
    Posts
    1,181
    Rep Power
    0

    Default

    U cant if you are using sql server. What you can do is save the path where the pic will be saved. Ur .net program can then access the pic based on the path it gets from sql server. In other words, the path is like a url for the pic, except that the pic will be saved locally on the server.
    'If we're supposed to work in Hex, why have we only got A fingers?'

    Follow Me: @psybuck2002us

  3. #3
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Au contraire, it is possible to store an image in your DB

    http://articles.techrepublic.com.com...ml#Listing%20B


    I'll look around some more to see if I can find something more specific. But for now maybe you can find something there
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  4. #4
    Join Date
    Nov 2004
    Posts
    41
    Rep Power
    0

    Default

    Yes u can store images to THE DB BUT
    The main advantages of storing the binary data on the file system are:
    • Ease of implementation – Storing and retrieving binary data stored directly within the database involves a bit more code than when working with data through the file system. Additionally, in order for a user to view or download binary data they must be presented with a URL to that data. If the data resides on the web server’s file system, the URL is straightforward. If the data is stored in the database, however, a web page needs to be created that will retrieve and return the data from the database.
    • Wider access to the binary data –the binary data may need to be accessible to other services or applications, ones that cannot pull the data from the database. For example, the images associated with each product might also need to be available to users through FTP, in which case we’d want to store the binary data on the file system.
    • Performance – if the binary data is stored on the file system, the demand and network congestion between the database server and web server will be less than if the binary data is stored directly within the database.
    The main disadvantage of storing binary data on the file system is that it decouples the data from the database. If a record is deleted from the table, the associated file on the web server’s file system is not automatically deleted

  5. #5
    Join Date
    Feb 2005
    Posts
    85
    Rep Power
    0

    Default

    Quote Originally Posted by itpccc View Post
    How do I Add photo records in sql through VB .net
    Code:
    '1. create field(of type image) in sql server for the photo
    
    '2.'bind the picture to a picture box on the form load event
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    Try
                    ''' Add bindings for the picture box
                    Dim b As Binding = New Binding("Image", mydataset, "tblCompanyInfo.Logo")
                    AddHandler b.Format, AddressOf ConvertPicture
                    Me.PictureBoxLogo.DataBindings.Add(b)
                    Me.PictureBoxLogo.SizeMode = PictureBoxSizeMode.StretchImage
                    Me.PictureBoxLogo.ClientSize = New Size(112, 104)
                    '''
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Binding Picture")
                End Try
                 
    
    
            Catch expSQL As SqlException
                MessageBox.Show(expSQL.ToString, Me.Text, _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try
    
    End Sub
    
       Private Sub ConvertPicture(ByVal sender As System.Object, ByVal e As ConvertEventArgs)
            'converts a byte array to image
    
            If Not e.Value Is DBNull.Value Then
                Dim b() As Byte
                b = e.Value
                Dim ms As MemoryStream = New MemoryStream
                ms.Write(b, 0, b.Length)
                Dim bmp As Bitmap = New Bitmap(ms)
                ms.Close()
                e.Value = bmp
            Else
    	    'an error will occur if you dont supply some form of dummy picture
    	    'therefore create a blank picture for the initial startup
    	    'then you can repalce it with anyone you like
                If File.Exists(Application.StartupPath & "\Blank.jpg") Then
                    Dim bmp As Bitmap = New Bitmap(Application.StartupPath & "\Blank.jpg")
                    e.Value = bmp
                Else
                    MsgBox(Application.StartupPath & "\Blank.jpg Is Missing", MsgBoxStyle.Critical, "Error")
                End If
            End If
            
            
        End Sub
    
    '3.Convert the picture to binary and save it to the database
    'convert the picture to binary for storage
                If File.Exists(Me.txtLogo.Text) Then
    
                    Dim fs As New FileStream(Me.txtLogo.Text, FileMode.Open, FileAccess.Read)
                    Dim br As New BinaryReader(fs)
                    Dim row As DataRow
                    row = mydataset.Tables(0).Rows(0)
                    row.BeginEdit()
                    row("logo") = br.ReadBytes(br.BaseStream.Length)
                    row.EndEdit()
    
                    br.Close()
                    fs.Close()
    
                    br = Nothing
                    fs = Nothing
                End If
    
                mybmb.EndCurrentEdit()
                mydataAdapter.Update(mydataset, "tblCompanyInfo")
                MsgBox("The Information Was Sucessfully Updated", MsgBoxStyle.Information, "Information")
    Last edited by Arch_Angel; Jul 17, 2007 at 02:03 PM. Reason: added CODE tags

  6. #6
    Join Date
    Jul 2006
    Posts
    276
    Rep Power
    0

    Default lol

    Psybuck for a senior techie u really dont kno ur stuff, toxic teach him
    The greatest discovery of all men through lifetime is that a man can alter his life by altering his attitude!

  7. #7
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by ToxXxic View Post
    Code:
    '2.'bind the picture to a picture box on the form load event
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    Try
                    ''' Add bindings for the picture box
                    Dim b As Binding = New Binding("Image", mydataset, "tblCompanyInfo.Logo")
                    AddHandler b.Format, AddressOf ConvertPicture
                    Me.PictureBoxLogo.DataBindings.Add(b)
                    Me.PictureBoxLogo.SizeMode = PictureBoxSizeMode.StretchImage
                    Me.PictureBoxLogo.ClientSize = New Size(112, 104)
                    '''
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Binding Picture")
                End Try
                 
    
    
            Catch expSQL As SqlException
                MessageBox.Show(expSQL.ToString, Me.Text, _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try
    
    End Sub
    
       Private Sub ConvertPicture(ByVal sender As System.Object, ByVal e As ConvertEventArgs)
            'converts a byte array to image
    
            If Not e.Value Is DBNull.Value Then
                Dim b() As Byte
                b = e.Value
                Dim ms As MemoryStream = New MemoryStream
                ms.Write(b, 0, b.Length)
                Dim bmp As Bitmap = New Bitmap(ms)
                ms.Close()
                e.Value = bmp
            Else
    	    'an error will occur if you dont supply some form of dummy picture
    	    'therefore create a blank picture for the initial startup
    	    'then you can repalce it with anyone you like
                If File.Exists(Application.StartupPath & "\Blank.jpg") Then
                    Dim bmp As Bitmap = New Bitmap(Application.StartupPath & "\Blank.jpg")
                    e.Value = bmp
                Else
                    MsgBox(Application.StartupPath & "\Blank.jpg Is Missing", MsgBoxStyle.Critical, "Error")
                End If
            End If
            
            
        End Sub


    That code kinda looked familiar... what happened to the good old days of giving credit. good coding still
    Last edited by icymint3; Jul 20, 2007 at 03:07 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •