[Translation] Save pictures to SQL 2000 Server database

xiaoxiao2021-03-06  14

Original source: http://www.codeproject.com/aspnet/PicManager.asp

Save the picture to SQL 2000 Server database Author: vivekthangaswamy how to upload files in ASP.NET Web pages? How to use ADO.NET technology to read a picture from the database and display on the web page?

Summary

.NET is a new type of distributed computing platform developed by Microsoft, ASP.NET is a programming mode for web development. The purpose of this article is to get some good experience in developing data-driven ASP.NET web applications. This app will tell you how to save a picture into the database and how to read images from the database. It uses ADO.NET as a data access mechanism, C # as a programming language, SQL 2000 Server as a background database.

Overview

General, large image files are often saved in the folder of the web server, not in the database. In some instances, the banking system is as an example, people first make the user's signature into a picture file and save it to the database.

Database mode

In this demonstration, Microsoft's SQL 2000 Server is used as a background database. I use a relatively special data type image. This image data type is used to save images to the database.

The controls used: system.web.ui.htmlControls.htmlinputFile System.Web.ui.WebControls.textBox System.Web.ui.WebControls.Button

The namespace used:

Using system.data.sqlclient;

Using system.drawing;

Using system.data;

Using system.io;

Using system.drawing.image;

coding

With the HTMLINPUTFILE class, it can declare an instance with the tag. The following example is a complete ASPX file that allows the user to upload the image file and the description of the image. Onupload method The picture and instructions write to the Picture table of the ISENSE database.

/ / Save the image file to the source code of 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 picture and explain it 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 functions created above can be called by using the button's onclick property.

How do I use ADO.NET technology to read images from the database and display it on the web page?

Here, I use the web page to display pictures without any other controls. The following code is used to display a picture in the database.

Private Void Page_Load (Object Sender, System.EventArgs E)

{

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

}

}

The GDI function provides a rich set of functions for operation and definition images. The examples of this article can only see a small part of it. You can call these features using System.drawing and System.drawing.Imaging namespace. For example, you can develop applications for saving and managing image files on the web, or you can make a simple, easy-to-configure application enable users to operate pictures.

How to run the program?

First, create a virtual directory and put your project file in this virtual directory. Then change the name, database name, and table name, as shown below:

SqlConnection Connection = New SQLCONNECTION

("Server = localhost; database = mypictures; uid = sa; pwd =");

Then publish your project to get the best results.

转载请注明原文地址:https://www.9cbs.com/read-47123.html

New Post(0)