C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to Retrieve Store image from the Database


Posted Date: 21 May 2008    Resource Type: Code Snippets    Category: Winforms Controls
Author: Siva PrasadMember Level: Gold    
Rating: Points: 5



  
SqlConnection connection = new SqlConnection("connectionString");
string commandText = "Select [image] from Image Where id=" + textBox1.Text;
connection.Open ();
SqlCommand cmd = new SqlCommand ( commandText, connection );
byte[] image = null;
image= ( byte[] ) cmd.ExecuteScalar ();
connection.Close ();
string strfn=Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream ( strfn, FileMode.CreateNew, FileAccess.Write );
fs.Write ( image, 0, image.Length );
fs.Flush ();
fs.Close ();
pictureBox1.Image = Image.FromFile ( strfn );




Responses

Author: Venkat Tammineni    26 Nov 2008Member Level: Gold   Points : 2
hi

'*Setup:
'*Create a form and place 3 command buttons named:
'*cmdLoad, cmdSelectSave, and cmdClear
'*Place a CommonDialog Control Named Dialog
'*Place an ImageBox (or PictureBox) named Image1


'** The field type in Sql Server must be "Image"
'** Everywhere you see "***" in the code is where you must enter
'** your own data.

Private Sub cmdClear_Click()
Image1.Picture = Nothing

End Sub

Private Sub cmdLoad_Click()
If Not LoadPictureFromDB(rstRecordset) Then
MsgBox "Invalid Data Or No Picture In DB"
End If
End Sub

Private Sub cmdSelectSave_Click()
'Open Dialog Box
With dlgDialog
.DialogTitle = "Open Image File...."
.Filter = "Image Files (*.gif; *.bmp)| *.gif;*.bmp"
.CancelError = True
procReOpen:
.ShowOpen

If .FileName = "" Then
MsgBox "Invalid filename or file not found.", _
vbOKOnly + vbExclamation, "Oops!"
GoTo procReOpen
Else
If Not SavePictureToDB(rstRecordset, .FileName) Then
MsgBox "Save was unsuccessful :(", vbOKOnly + _
vbExclamation, "Oops!"
Exit Sub
End If
End If

End With
End Sub

Private Sub Form_Load()
Set cnnConnection = New ADODB.Connection
Set rstRecordset = New ADODB.Recordset

cnnConnection.Open ("Provider=SQLOLEDB; " & _
"data Source=**YourServer**;" & _
"Initial Catalog=**YourDatabase**; " & _
"User Id=**YourUID**;Password=***YourPass***")
rstRecordset.Open "Select * from YourTable", cnnConnection, _
adOpenKeyset, adLockOptimistic


End Sub


Public Function LoadPictureFromDB(RS As ADODB.Recordset)

On Error GoTo procNoPicture

'If Recordset is Empty, Then Exit
If RS Is Nothing Then
GoTo procNoPicture
End If

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open

strStream.Write RS.Fields("**YourImageField**").Value


strStream.SaveToFile "C:\Temp.bmp", adSaveCreateOverWrite
Image1.Picture = LoadPicture("C:\Temp.bmp")
Kill ("C:\Temp.bmp")
LoadPictureFromDB = True

procExitFunction:
Exit Function
procNoPicture:
LoadPictureFromDB = False
GoTo procExitFunction
End Function

Public Function SavePictureToDB(RS As ADODB.Recordset, _
sFileName As String)

On Error GoTo procNoPicture
Dim oPict As StdPicture

Set oPict = LoadPicture(sFileName)

'Exit Function if this is NOT a picture file
If oPict Is Nothing Then
MsgBox "Invalid Picture File!", vbOKOnly, "Oops!"
SavePictureToDB = False
GoTo procExitSub
End If

RS.AddNew


Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sFileName
RS.Fields("***YourImageField***").Value = strStream.Read

Image1.Picture = LoadPicture(sFileName)
SavePictureToDB = True



procExitSub:
Exit Function
procNoPicture:
SavePictureToDB = False
GoTo procExitSub
End Function



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Read Data from an XML File into a Listbox using C#
Previous Resource: Closing Notepad using C#.Net application
Return to Discussion Resource Index
Post New Resource
Category: Winforms Controls


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use