how to save image file into sql database
-
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
-
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
'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 -
'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 -
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
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!