Results 1 to 5 of 5

Thread: Adding Image in SQL database through vb.net application?

  1. #1
    Join Date
    Jan 2009
    Posts
    19

    Adding Image in SQL database through vb.net application?

    Hi,
    I want to know How can I add image to the SQL database through the form in my Vb.Net project.

    Can you guys please help me with the same?

    Regards,

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Re: Adding Image in SQL database through vb.net application?

    This source code is use to insert picture to SQL Server to binary format that is make the database....

    Code:
    Function SaveImage _
       (ByVal FileName As String) As Integer
    
       ' Create a FileInfo instance
       ' to retrieve the files information
       Dim fi As New FileInfo(FileName)
    
       ' Open the Image file for Read
       Dim imgStream As Stream = _
          fi.OpenRead
    
       ' Create a Byte array the size
       ' of the image file for the Read
       ' methods buffer() byte array
       Dim imgData(imgStream.Length) As Byte
       imgStream.Read(imgData, 0, fi.Length)
    
       Dim cn As New SqlConnection _
          (ConfigurationSettings.AppSettings("cn"))
       Dim cmd As New SqlCommand("Images_ins", cn)
       cmd.CommandType = CommandType.StoredProcedure
    
       With cmd.Parameters
          .Add("@FileName", VarChar, 100).Value = _
             fi.Name
          .Add("@FileType", VarChar, 10).Value = +
             fi.Extension
          .Add("@Image", Image).Value = imgData
          .Add("@FileSize", Int, 4).Value = fi.Length
       End With
    
       cn.Open()
       cmd.ExecuteNonQuery()
       cn.Close()
       cn.Dispose()
    End Function

  3. #3
    Join Date
    Apr 2008
    Posts
    2,010

    Re: Adding Image in SQL database through vb.net application?


  4. #4
    Join Date
    May 2008
    Posts
    40

    Re: Adding Image in SQL database through vb.net application?

    ASP.NET/VB.NET Code to insert images in MS SQL Server Database.

    Follow this link!

    http://www.dotnetjunkies.com/WebLog/...les/39751.aspx

  5. #5
    Join Date
    May 2008
    Posts
    115

    Re: Adding Image in SQL database through vb.net application?

    Here is the code to insert images to a Ms Access database!

    Code:
    File name is Image.vb
    Imports System
    Imports System.IO
    Imports System.Data
    Public Class SaveImage
    Shared Sub main()
    Dim o As System.IO.FileStream
    Dim r As StreamReader
    Dim gifFile As String
    Console.Write("Enter a Valid .Gif file path")
    gifFile = Console.ReadLine
    If Dir(gifFile) = "" Then
       Console.Write("Invalid File Path")
       Exit Sub
    End If
    o = New FileStream(gifFile, FileMode.Open, FileAccess.Read, FileShare.Read)
    r = New StreamReader(o)
    Try
    Dim FileByteArray(o.Length - 1) As Byte
    o.Read(FileByteArray, 0, o.Length)
    Dim Con As New _ System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data 
    Source=Test.mdb")
    Dim Sql As String = "INSERT INTO Images (Pic,FileSize) VALUES (?,?)"
    Dim Cmd As New System.Data.OleDb.OleDbCommand(Sql, Con)
    Cmd.Parameters.Add("@Pic", System.Data.OleDb.OleDbType.Binary, o.Length).Value = FileByteArray
    Cmd.Parameters.Add("@FileSize", System.Data.OleDb.OleDbType.VarChar, 100).Value = o.Length
    Con.Open()
    Cmd.ExecuteNonQuery()
    Con.Close()
    Catch ex As Exception
    Console.Write(ex.ToString)
    End Try
    End Sub
    End Class
    A file will be inserted in the Database each time the code is executed.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2011, 03:02 AM
  2. Warning message when adding data to database
    By Jagavi in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 03:40 PM
  3. Problem regarding the adding my thumbnail image to file
    By reetus in forum Windows Software
    Replies: 4
    Last Post: 14-12-2009, 04:23 PM
  4. How to refresh database after adding a new record?
    By ArunJ in forum Software Development
    Replies: 5
    Last Post: 16-02-2009, 06:27 PM
  5. Insert image in access database from vb 6.0
    By Vireshh in forum Software Development
    Replies: 2
    Last Post: 22-01-2009, 08:06 PM

Posting Permissions

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