|
-
Apr 6th, 2011, 10:51 AM
#1
Thread Starter
Lively Member
Insert Image in SQL DB
Hello everyone .
The thing is as follows:
- 1 picture box
- 1 button
- on_click picturebox = loads image from HDD with OpenFileDialog
- on button = validates if there is a pic in PictureBox.Image
saves PictureBox.Image in DB
I know how to do everything except saving the in the DB.
I am using SQL server 2008 DB .
Any help ?
Thanks in advance.
Best Regards .
-
Apr 6th, 2011, 08:36 PM
#2
Re: Insert Image in SQL DB
Follow the CodeBank link in my signature and check out my thread dedicated to just that.
-
Apr 7th, 2011, 04:05 AM
#3
Thread Starter
Lively Member
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)
-
Apr 7th, 2011, 04:14 AM
#4
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.
-
Apr 7th, 2011, 04:26 AM
#5
Thread Starter
Lively Member
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 ?
-
Apr 7th, 2011, 04:31 AM
#6
Re: Insert Image in SQL DB
 Originally Posted by HelderMPinhal
Any idea why it would do that ?
Um, yeah. The idea in my previous post.
-
Apr 7th, 2011, 04:32 AM
#7
Re: Insert Image in SQL DB
 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.
-
Apr 7th, 2011, 08:03 AM
#8
Thread Starter
Lively Member
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|