Hi everyone! I am a junior database programmer. I need to know how can I save image in SQL SERVER 2000 by VB.NET? I made a table in SQL SERVER with two fields. One is Id[nvarchar] & another pic[nvarchar]. I got two forms there in VB.NET project. In the first form I got Picture Box[PictureBox], Text Box[txt1] & two buttons[Button1 & Button2]. I got one module there in my project.
Module code :
Public con As New ADODB.Connection
Public rst As New ADODB.Recordset
Button1 code :
Dim dlg As OpenFileDialog
Dim img As Image
Try
dlg = New OpenFileDialog
dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg;*.png|" & _
"Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png"
If dlg.ShowDialog = DialogResult.OK Then
Me.Cursor = Cursors.WaitCursor
PictureBox.Image = New Bitmap(dlg.FileName)
End If
Catch ex As Exception
Finally
img = Nothing
dlg = Nothing
Me.Cursor = Cursors.Default
End Try
[B][U]Button2 code :[/U][/B] con.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test")
rst = New ADODB.Recordset
With rst
.Open("Select * from pic_info", con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
.AddNew()
.Fields("Id").Value = txt1.Text
.Fields("pic").Value = PictureBox.Image
.Update()
.Close()
End With
Result: It saves Id number. I guess it also saves image because when I open pic_info table from SQL SERVER 2000 it shows "System.Drawing.Bitmap" there in the pic field.
Second form:
I got one Combo Box[cmbid], one Picture Box[picbox] & one button[Button1].
Form load code :
cmbid.Items.Clear()
con = New ADODB.Connection
con.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test")
rst = New ADODB.Recordset
With rst
.Open("Select * From pic_info", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
If .BOF = False Then
.MoveFirst()
While .EOF = False
cmbid.Items.Add(.Fields("Id").Value)
.MoveNext()
End While
End If
.Close()
End With
[B][U]Button1 code :[/U][/B] rst = New ADODB.Recordset
With rst
.Open("Select pic From pic_info where Id = '" & cmbid.Text & "'", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
If .BOF = False Then
.MoveFirst()
While .EOF = False
picbox.Image = (.Fields("pic").Value)
.MoveNext()
End While
End If
.Close()
End With
Result : It shows error message "Unable to cast object of type 'System.String' to type 'System.Drawing.Image'" when I run this project.
So I need help. Please help me. I need to know how can I save & retrieve image in SQL SERVER 2000 by VB.NET easily.