Bhutanesedude
New member
- Joined
- Jun 12, 2013
- Messages
- 4
- Programming Experience
- Beginner
Help needed. I am newbie on VB.NET and looking to upload Image to DB converting to Byte.
I have a BD where in a table named "tbemp" with fields "eid, name, image".
My application have a Database Access file DBAccess.vb with the following code:
Now I also have a function to convert the image file to Byte (Got it from google) as:
Now when I click on save button, I usually use this way of code to save data into Data Base:
Now my question is how can I save the image file (converted into Byte from the function above) into Database using the above like qry? Please help me.
I have a BD where in a table named "tbemp" with fields "eid, name, image".
My application have a Database Access file DBAccess.vb with the following code:
VB.NET:
Imports System.Data.SqlClientImports System.Data
Imports Microsoft.VisualBasic
Public Class DBAccess
Shared cs As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath.Replace("\bin\Debug", "") & "\eis.mdf;Integrated Security=True;User Instance=True")
Public Shared Function SaveData(ByVal qry As String) As Boolean
Dim cmd As New SqlCommand(qry, cs)
Try
cs.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
Finally
cs.Close()
End Try
End Function
Public Shared Function FetchData(ByVal qry As String) As DataSet
Dim cmd As New SqlCommand(qry, cs)
Dim ds As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(ds)
Return ds
End Function
Public Shared Function saveGrid(ByVal query As String, ByVal ds As DataSet) As Boolean
Try
Dim da As New SqlDataAdapter(query, cs)
Dim cb As New SqlCommandBuilder(da)
da.InsertCommand = cb.GetInsertCommand
da.UpdateCommand = cb.GetUpdateCommand
da.DeleteCommand = cb.GetDeleteCommand
da.Update(ds)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
Now I also have a function to convert the image file to Byte (Got it from google) as:
VB.NET:
Public Function convertimage(ByVal myImage As Image) As Byte() Dim mstream As New MemoryStream()
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(myBytes, 0, mstream.Length)
Return myBytes
End Function
Now when I click on save button, I usually use this way of code to save data into Data Base:
VB.NET:
Dim qry As String
qry = "INSERT INTO tbemp values ('" + eid.Text + "','" + name.Text + "');"
If DBAccess.SaveData(qry) Then
MessageBox.Show("Data Saved", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Hide()
Else
MessageBox.Show("Error saving", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
End If
Now my question is how can I save the image file (converted into Byte from the function above) into Database using the above like qry? Please help me.