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 !




Storing Image into the Database


Posted Date: 21 May 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: Siva PrasadMember Level: Gold    
Rating: Points: 5



How to store images into the databse

OpenFileDialog op = new OpenFileDialog ();
op.ShowDialog ( this );
string imageName = op.FileName;
this.pictureBox1.Image = Image.FromFile ( imageName );
long imageLength = op.FileName.Length;
FileInfo fi = new FileInfo ( imageName );
byte[] byteArray;
imageLength = fi.Length;
FileStream fs = new FileStream ( imageName, FileMode.Open, FileAccess.Read, FileShare.Read );
byteArray = new byte[ Convert.ToInt32 ( imageLength ) ];
int byteRead = fs.Read ( byteArray, 0, Convert.ToInt32 ( imageLength ) );
fs.Flush ();
fs.Close ();
SqlConnection connection = new SqlConnection("connectionString");
string commandText = "Insert into Image values ( @image )";
SqlCommand cmd = new SqlCommand ( commandText, connection );
cmd.Parameters.Add ( "@image", SqlDbType.Image ).Value = byteArray;
connection.Open ();
cmd.ExecuteNonQuery ();
connection.Close ();




Responses

Author: Nagamohan kumar P    24 May 2008Member Level: Gold   Points : 2
this is cool ,but could you please say how can we retrieve the image from the database ?


Author: Siva Prasad    26 May 2008Member Level: Gold   Points : 2
// Code for Retrieving Images from Database

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 );

Siva Prasad


Author: komaladevi    26 May 2008Member Level: Gold   Points : 2
i have given the code to retreive the image from database


Author: syed shakeer hussain     01 Dec 2008Member Level: Gold   Points : 0
hi


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: How to Add Auto Increment Column to DataSet
Previous Resource: SavePoint of Transaction in ADO.Net
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use