Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to save image file into sql database

how to save image file into sql database

Scheduled Pinned Locked Moved Visual Basic
databasehelptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thepityone
    wrote on last edited by
    #1

    hello,,, please can you help me about this problem cause i dont know how to save an image into the sql database and how i'm going to code for it....i'm a novice programmer and i'm eager to learn about it can please help me to solve this problem..please... thanks

    R A 2 Replies Last reply
    0
    • T thepityone

      hello,,, please can you help me about this problem cause i dont know how to save an image into the sql database and how i'm going to code for it....i'm a novice programmer and i'm eager to learn about it can please help me to solve this problem..please... thanks

      R Offline
      R Offline
      Rana Muhammad Javed Khan
      wrote on last edited by
      #2

      'This code can b used to store image in database using VB 6.0 Private Sub StoreImage(sPath As String) Dim bytBLOB() As Byte Dim intNum As Integer Dim rs as new RecordSet If (Trim(sPath) <> "") Then rs.Open "Select * From tTable1",cn,,adOpenDynamic,adLockOptimistic 'Start store pic intNum = FreeFile Open sPath For Binary As #intNum ReDim bytBLOB(FileLen(Trim(sPath))) Get #intNum, , bytBLOB Close #1 'End store pic rs.AddNew rs("Img").AppendChunk bytBLOB 'Other Fields...." rs.Update End If End Sub Regards, Javed

      C 1 Reply Last reply
      0
      • R Rana Muhammad Javed Khan

        'This code can b used to store image in database using VB 6.0 Private Sub StoreImage(sPath As String) Dim bytBLOB() As Byte Dim intNum As Integer Dim rs as new RecordSet If (Trim(sPath) <> "") Then rs.Open "Select * From tTable1",cn,,adOpenDynamic,adLockOptimistic 'Start store pic intNum = FreeFile Open sPath For Binary As #intNum ReDim bytBLOB(FileLen(Trim(sPath))) Get #intNum, , bytBLOB Close #1 'End store pic rs.AddNew rs("Img").AppendChunk bytBLOB 'Other Fields...." rs.Update End If End Sub Regards, Javed

        C Offline
        C Offline
        campbells
        wrote on last edited by
        #3

        is it able to store the image file to a txt file? or dat file?

        1 Reply Last reply
        0
        • T thepityone

          hello,,, please can you help me about this problem cause i dont know how to save an image into the sql database and how i'm going to code for it....i'm a novice programmer and i'm eager to learn about it can please help me to solve this problem..please... thanks

          A Offline
          A Offline
          alien viper
          wrote on last edited by
          #4

          Private Sub SaveImageToSql() 'Try this example. 'This is not the best way but if you have no other way, try it. '********************************************* 'You must StoredProcedure like this '@Image image '"INSERT INTO Table ([Image]) VALUES (@Image)" '********************************************* Dim connStr As String = "Connection String" ' Set Your Connection String Dim filePath As String = "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg" ' Set Your Image File Path Dim fs As IO.FileStream = New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read) Dim br As New IO.BinaryReader(fs) Dim bytes() As Byte = br.ReadBytes(fs.Length) Dim sqlImg As New SqlTypes.SqlBinary(bytes) Dim conn As New SqlClient.SqlConnection(connStr) Dim cmd As New SqlClient.SqlCommand("StoredProcedureName", conn) Try Dim sqlP As New SqlClient.SqlParameter("@Image", SqlDbType.Image) sqlP.Value = sqlImg cmd.Parameters.Add(sqlP) conn.Open() cmd.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) Finally conn.Close() conn.Dispose() cmd.Dispose() br.Close() fs.Close() br = Nothing fs = Nothing bytes = Nothing End Try End Sub !alien!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups