Re: Insert Image in SQL DB
Follow the CodeBank link in my signature and check out my thread dedicated to just that.
Re: Insert Image in SQL DB
vb Code:
Imports System.Data.SqlClient
Public Class Form1
Dim File As String
Private Sub FotografiaBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FotografiaBox.Click
GetImage.Filter = "Image Files (BMP, JPEG, PNG)|*.bmp;*.jpg;*.jpeg;*.png"
GetImage.ShowDialog()
Try
FotografiaBox.Image = Image.FromFile(GetImage.FileName)
File = GetImage.FileName
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnection As New SqlConnection("Server = " & "INSYSGAMEFORCE\SPESCOLAS; Database = Escola; Integrated Security = sspi;")
Dim command As New SqlCommand("INSERT INTO teste VALUES(@Picture)", myConnection)
'Create an Image object.
Using picture As Image = Image.FromFile(File)
'Create an empty stream in memory.
Using stream As New IO.MemoryStream
'Fill the stream with the binary data from the Image.
picture.Save(stream, Imaging.ImageFormat.Jpeg)
'Get an array of Bytes from the stream and assign to the parameter.
command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
End Using
End Using
Try
myConnection.Open()
command.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I am using the code above to insert the image . In your example you had an UPDATE command but i need an INSERT so I modified it . The problem is that when I click the button it sais "string or binary data would be truncated. The statement has been terminated."
Can you help me with this pls ?
The table I am using is
Table name: teste
Fields:
ID - which is int auto inc (+1)
Imagem - which in varbinary(100)
Re: Insert Image in SQL DB
Is your image 100 bytes or less in size? If not then it's not going to fit into a varbinary column limited to 100 bytes.
Re: Insert Image in SQL DB
I tried your UPDATE code too and it still gives me the same error message .
Any idea why it would do that ?
Re: Insert Image in SQL DB
Quote:
Originally Posted by
HelderMPinhal
Any idea why it would do that ?
Um, yeah. The idea in my previous post.
Re: Insert Image in SQL DB
Quote:
Originally Posted by
jmcilhinney
Is your image 100 bytes or less in size? If not then it's not going to fit into a varbinary column limited to 100 bytes.
jmc already told you what was wrong.
You have a 100 byte field.
Change the field definition to varbinary(max) - max is a keyword in this case indicating to create a truly large varbinary() field.
Re: Insert Image in SQL DB
When I did my last post my browser wasn't refreshed so I didn't see that post , sorry .
But thanks everyone for your help :)