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 » Articles » ASP.NET/Web Applications »

Save The Image Into SQL Server 2000 Database


Posted Date: 03 Mar 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: vivekthangaswamyMember Level: Gold    
Rating: 1 out of 5Points: 10



Abstract:

.NET is the new distributed computing platform developed by Microsoft and ASP.NET is its programming model for web development. The intent of this article is to get a good experience in developing data driven ASP.NET web forms applications by means of a real world application. This application will teach you how to save the image file into the database and how to retrieve it from the database. It uses ADO.NET as the data access mechanism, C# as the development language and SQL Server 2000 as the backend Database.


Overview of Solution:

Normally images are saved in the web server folder not in the database; this is for larger file size category. In some cases like, in bank for example they use to scan the user signature as image file and save that file into the database.
· Database schema:
MS SQL Server 2000 is used as a backend database for this small demonstration. And I used special data type in SQL Server called image. The image data type is used to save the image into the database.
· Controls used in this application
1) System.Web.UI.HtmlControls.HtmlInputFile
2) System.Web.UI.WebControls.TextBox
3) System.Web.UI.WebControls.Button
· Namespace used in this application
1) using System.Data.SqlClient;
2) using System.Drawing;
3) using System.Data;
4) using System.IO;
5) using System.Drawing.Imaging;


Solution with Code:

Use the HtmlInputFile class, which you can declare an instance of with an <input type="file" runat="server"/> tag. The following example is a complete ASPX file that lets a user upload an image file and a comment describing the image. The OnUpload method writes the image and the comment to a table named Pictures in a SQL Server database named iSense.


How do I read an image from a database using ADO.NET and display it in a Web page?

Use the HtmlInputFile class, which you can declare an instance of with an <input type="file" runat="server"/> tag. The following example is a complete ASPX file that lets a user upload an image file and a comment describing the image. The OnUpload method writes the image and the comment to a table named Pictures in a SQL Server database named iSense.
Here in this I used the web page to display the image and not any other control. The following is the code for display the image from the database.



The GDI+ functions offer a rich set of features for managing and modifying image data. This article's examples offer only a glimpse into the functionality you can leverage using the classes available in the System.Drawing and System.Drawing.Imaging namespaces. For example, you can develop applications for storing and managing image files on the Web, or you can provide a simple, easily deployed application that enables users to manipulate images.

How to Run this application? First create a virtual directory and put this project files into the virtual directory. And than change the server name, database name, and table name in the following statement.
SqlConnection connection = new SqlConnection
("server=localhost;database=mypictures;uid=sa;pwd=");
and publish the project you all get the best result.

// Source Code for Save the image file into the database

public void OnUpload(Object sender, EventArgs e)
{
// Create a byte[] from the input file
int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read (pic, 0, len);
// Insert the image and comment into the database
SqlConnection connection = new SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
try
{
connection.Open ();
SqlCommand cmd = new SqlCommand ("insert into Image " + "(Picture, Comment) values (@pic, @text)", connection);
cmd.Parameters.Add ("@pic", pic);
cmd.Parameters.Add ("@text", Comment.Text);
cmd.ExecuteNonQuery ();
}
finally
{
connection.Close ();
}

}
The above created function is called using the onClick property of a button.


// Source Code for Retrieving the image file from the database

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
MemoryStream stream = new MemoryStream ();
SqlConnection connection = new SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
try
{
connection.Open ();
SqlCommand command = new SqlCommand ("select Picture from Image", connection);
byte[] image = (byte[]) command.ExecuteScalar ();
stream.Write (image, 0, image.Length);
Bitmap bitmap = new Bitmap (stream);
Response.ContentType = "image/gif";
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
}
finally
{
connection.Close ();
stream.Close ();
}
}


Bibliography:

Ø WWW.MDSN.COM

by
Vivek.T
Technology Toolbox: C#, ASP.NET






Responses

Author: Siddesh Kapadi    14 Apr 2005Member Level: Bronze   Points : 0
The above code doesnot work as some of the properties are not available and the program gives an error during compilation.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add 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: Format DateTime Values in XML
Previous Resource: URL rewriting When FormsAuthentication enabled
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use