C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Images From the Database


Posted Date: 26 Oct 2009    Resource Type: Code Snippets    Category: File Operations
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 15 (Rs 5)



Daily I am seeing atlest one question about load the images from the Database.
So I write one class for that. If anybody want to get the images from the database
just copy and past the following class and use it.

Code for the class ImagesFromDatabase


public class ImagesFromDatabase{
string MyimageFilename = null;
byte[] MyimageBytes = null;

SqlConnection MySqlConnection = null;
SqlCommand MySqlCommand = null;
SqlDataReader MySqlDataReader = null;

public ImagesFromDatabase() {
MySqlConnection = new SqlConnection("Your Connection String");
MySqlCommand = new SqlCommand(@"select MyFileName, MyFileData from MyTable", MySqlConnection);

MySqlConnection.Open();
MySqlDataReader = MySqlCommand.ExecuteReader();
}

public Bitmap GetImage() {
MemoryStream MyMemoryStream = new MemoryStream(MyimageBytes);
Bitmap MyBitmap = new Bitmap(MyMemoryStream);
return MyBitmap;
}

public string GetFilename() {
return MyimageFilename;
}

public bool HaveImages() {
if (MySqlDataReader.Read()) {
MyimageFilename = (string) MySqlDataReader.GetValue(0);
MyimageBytess = (byte[]) MySqlDataReader.GetValue(1);
return true;
}else {
return false;
}
}

public void CloseObjects() {
MySqlDataReader.Close();
MySqlConnection.Close();
}
}



Execution part of the above class as follows

ImagesFromDatabase MyImages= new ImagesFromDatabase();
if (MyImages.HaveImages()) {
this.textBox1.Text = MyImages.GetFilename();
this.pictureBox1.Image = (Image) MyImages.GetImage();
}
MyImages.CloseObjects()


Code Explanation

1. Create instance of the class SqlConnection, SqlCommand ,SqlDataReader.
2. In the constructor get all the recordes from the database using the SqlDataReader
3. HaveImages() method read the filename and iamge byte array from the database
4. GetImage() method will convert the byte array into the bitmap using memory stream
5. GetFilename() method will display the file name of the image
6. CloseObjects() method will close sqlconnecions.
7. In the implementation part you can see the usage of the class clearly


By
Nathan




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Images From the Database  .  

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: Decode your file using UTF8
Previous Resource: One movement with IsolatedStorage class
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use