PC Review


Reply
Thread Tools Rate Thread

How do I dump Image data type in SQL Server

 
 
BJ
Guest
Posts: n/a
 
      8th Aug 2005
I have a column, of data type image, in a SQL table that I'm trying to
dump to a windows form control.

Does anyone have an example of this code and what control I would use
on the form?

-BJ

 
Reply With Quote
 
 
 
 
BJ
Guest
Posts: n/a
 
      8th Aug 2005
Found it. Wouldn't you know it right after I posted my request

http://support.microsoft.com/default...B;EN-US;321900

 
Reply With Quote
 
BJ
Guest
Posts: n/a
 
      8th Aug 2005
CODE NOT WORKING

Ok from the example I was able to come up with the following code and I
get this error:

ERROR =
'System.ArgimentException' occured in system.drawing.dll
Invalid parameter used

CODE =
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlConnection = New
SqlConnection("DATABASE=MyDB;SERVER=MyServer;USER
ID=Bryan;PASSWORD=hertrim")
#Region " Windows Form Designer generated code "
#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cmd As SqlCommand = New SqlCommand("SELECT BlobCol FROM
MySQLTable", cn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet

Dim c As Integer

da.Fill(ds, "dsTable")
c = ds.Tables("dsTable").Rows.Count

If c > 0 Then
Dim bytBLOBData() As Byte = ds.Tables("dsTable").Rows(c -
1)("BlobCol")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PBOX.Image = Image.FromStream(stmBLOBData)
End If
MsgBox("Image retrieved")

ds = Nothing
da = Nothing

cn.Close()
cn = Nothing

End Sub
End Class

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      9th Aug 2005
BJ,

Have a look at this sample on our website, there is more.

http://www.windowsformsdatagridhelp....6-38b5a2f7fdf0

I hope this helps,

Cor


 
Reply With Quote
 
BJ
Guest
Posts: n/a
 
      10th Aug 2005
Everthing works except the line where I assign the image to the
Picturebox.Image property. I take an invalid parm exception. See code
below. As a note, I was able to dump contents to a data file, but I
don't want to dump to a file and then turn around and post to the form.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim sqlcmd As New SqlCommand("SELECT ImageField FROM Mydb",
conn)
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim ms As New MemoryStream
Dim bytImage() As Byte


da.SelectCommand = sqlcmd

da.Fill(ds, "ByteTbl")

bytImage = CType(ds.Tables("ByteTbl").Rows(0)(0), Byte())

ms = New MemoryStream(bytImage)
Me.PBOX.Image = Image.FromStream(ms)

conn.Close()
conn = Nothing
sqlcmd = Nothing
da = Nothing
ds = Nothing
ms = Nothing
bytImage = Nothing

End Sub

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      10th Aug 2005
BJ,

Are you sure that you have a table in your database Mydb which you name in
your dataset suddenly ByteTbl.

As well (not the reason that it is not working). Why do you do all those
crazy setting to nothing, it does nothing beside processing some code? All
those objects go automaticly out of scope in that procedure.

I hope this helps,

Cor


 
Reply With Quote
 
BJ
Guest
Posts: n/a
 
      11th Aug 2005
The table connection is working. I tested the connection / fill /
table (ByteTbl) content by dumping a couple of other columns.

The content isn't the problem, because I can dump the contents to a
flat file and interrogate it. By the way the contents of each data
cell is a Crystal Reports template. Every example I've seen about
dumping an image column to a win form Picture Box uses the above code.
So I'm going to try to store my own jpeg images to a table and use the
code above to pull it out again. If that works, then the Picture Box
control probably has a problem storing that kind of data.

Those crazy lines that set my objects to nothing is (1) by habit from
my C/C++/Java days where is was always good practice to do your our
house keeping; (2) do you really trust MS to do all your housekeeping?
Have you ever known a product to have a memory leak? Basically, the
code does hurt, for ME its just a good programming habit.

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      11th Aug 2005
BJ

That good programming habit is as good as this

Sub A
dim a as integer
...
a = 0
End sub

However feel free to do this kind of code.
It is especially useful if you are paid per line.

And if it gives errors, than buy another compiler, if this is not done by
your compiler than it is really a bad product, in vendors who have these
compilers tell of course told that it is good practise.

Just my idea

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Image or Picture from SQL Image Data Type Sam K Microsoft Excel Programming 0 1st Dec 2008 11:04 PM
how to open Image data type (sql server) field in MS-Word using with Visual basic amit saini Microsoft VB .NET 1 23rd Jul 2008 11:23 AM
Macro to dump data from excel to sql server table mili07 Microsoft Excel Programming 0 18th Sep 2006 04:37 PM
Binary (Image) type fields of SQL Server msnews.microsoft.com Microsoft C# .NET 1 25th Apr 2004 02:37 PM
Re: Question about 'image' data type in SQL Server Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 1 14th Jul 2003 09:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:50 PM.