ASP. NET writes and reads images (C # + SQL Server)

Source: Internet
Author: User
Tags readfile

// 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.exe cutenonquery () = 1)
Response. Write ("successful ");
Else
Response. Write ("fail ");

Conn. Close ();
}
// 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 willCompositionCurrent output
Response. binarywrite (byte []) Reader ["image"]); // picture data
}
Response. Write ("successful ");
Response. End ();
Conn. Close ();
}

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////

// Other custom methods are as follows:

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

// get the file name
private string GetFile ()
{< br> httppostedfile file = file1.postedfile;
filename = file. filename;
return filename;
}< br> // read the file content
private byte [] readfile ()
{< br> filestream file = file. openread (GetFile ();
byte [] content = new byte;
file. read (content, 0, content. length);
file. close ();
return content;
}

// obtain the image suffix
private string getcontenttype (string extension)
{< br> string type = "";
If (extension. equals ("jpg") | extension. equals ("jpg")
type = "Jpeg";
else
type = extension;
return "image/" + type;
}

Related Article

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.