Write and read images (C # ASP. Net sqlserver)

Source: Internet
Author: User

First, you must create a table that contains the image and type fields.

Webform1.aspx

<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "readandwritepicfromdb. webform1" %> <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML>

 

Webform1.aspx. CS

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. Data. sqlclient;

Namespace readandwritepicfromdb
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. htmlcontrols. htmlinputfile file1;
Protected system. Web. UI. webcontrols. Button button1;
Private string filename = "";
Protected system. Web. UI. webcontrols. Button button2;
Private Static sqlconnection conn = NULL;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Connectdb ();
}

// Get the file name
Private string GetFile ()
{
Httppostedfile file = file1.postedfile;
Filename = file. filename;

Return filename;
}

// Read the file content
Private byte [] readfile ()
{
Filestream file = file. openread (GetFile ());
Byte [] content = new byte;
File. Read (content, 0, content. Length );
File. Close ();

Return content;
}

// Connect to the database
Private void connectdb ()
{
String connstr = "Initial catalog =; Data Source =; user id =; Password = ;";
Conn = new sqlconnection (connstr );
Conn. open ();
}

// Write the image to the database
Private void writeimage ()
{
Sqlcommand comm = conn. createcommand ();
Comm. commandtext = "insert into images (image, type) values (@ image, @ type )";
Comm. commandtype = commandtype. text;
Sqlparameter Param = comm. Parameters. Add ("@ image", sqldbtype. Image );
Param. value = readfile ();
Param = comm. Parameters. Add ("@ Type", sqldbtype. nvarchar );
Param. value = getcontenttype (New fileinfo (filename). extension. Remove (0, 1 ));

If (Comm. executenonquery () = 1)
Response. Write ("successful ");
Else
Response. Write ("fail ");

Conn. Close ();
}

// Get the suffix of the image
Private string getcontenttype (string extension)
{
String type = "";

If (extension. Equals ("jpg") | extension. Equals ("jpg "))
Type = "Jpeg ";
Else
Type = extension;

Return "image/" + type;
}

// Read images from the database
Private void ReadImage ()
{
Sqlcommand comm = conn. createcommand ();
Comm. commandtext = "select image, type from images ";
Comm. commandtype = commandtype. text;

Sqldatareader reader = comm. executereader ();
While (reader. Read ())
{
Response. contenttype = reader ["type"]. tostring (); // The read/write type must be set. Otherwise, the browser outputs the data as text.
Response. binarywrite (byte []) Reader ["image"]); // picture data
}

Response. Write ("successful ");
Response. End ();

Conn. Close ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Writeimage ();
}

Private void button2_click (Object sender, system. eventargs E)
{
Try
{
ReadImage ();
}
Catch (exception EP)
{
Conn. Close ();
Response. End ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.