Write and read pictures (C # ASP. NET SQL Server)

Source: Internet
Author: User
Tags readfile visual studio
Server|sqlserver first you want to create a table containing both image and Vnanchar
from the segment image and type types




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> <HEAD> <title>webform1</ title> <meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" > <meta name= "code_language" Content = "C #" > <meta name= "vs_defaultClientScript" content= "JavaScript" > <meta name= "vs_targetschema" content= " Http://schemas.microsoft.com/intellisense/ie5 "> </HEAD> <body ms_positioning=" GridLayout "> <form Id= "Form1" method= "POST" runat= "Server" > <font face= "Arial ><input id=" File1 "style=" Z-INDEX:101; left:112px; Position:absolute; top:88px "type=" file "Name=" File1 "runat=" Server "></FONT> <asp:button id=" Button1 "style=" z-index:102 "; left:400px; Position:absolute; top:88px "runat=" Server "text=" Write "width=" 104px "></asp:Button> <asp:button id=" Button2 "style= "z-index:103; left:552px; Position:absolute; top:88px "runat=" Server "text=" Read "width=" 105px "></asp:Button> </form> </body> </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 description of the
///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 page


Connectdb ();


}





//Get filename


private String GetFile ()


{


httppostedfile file = File1.PostedFile;


fileName = file. FileName;





return fileName;


}





//Read file contents


Private byte[] ReadFile ()


{


FileStream file = File.openread (GetFile ());


byte[] content = new Byte[file. Length];


file. Read (content,0,content. Length);


file. Close ();





return content;


}





//Connection Database


private void Connectdb ()


{


string connstr = "Initial catalog=;D ata source=; User id=; password=; ";


conn = new SqlConnection (CONNSTR);


Conn. Open ();


}





//write pictures 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 name of the picture


private String getContentType (string extension)


{


String type = "";





if (extension. Equals ("jpg") | | Extension. Equals ("JPG"))


type = "JPEG";


Else


type = extension;





return "image/" +type;


}





//Read the picture 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 ()//read/write type must be set otherwise the browser will be printed as text








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.