Data | Database development environment: Windows 2000, SQLServer2000,. Net Framework SDK Official edition
Development language: C #, ASP.net
Description: Image storage and Reading in database
Description: In ASP, we use Request.TotalBytes, Request.BinaryRead () to upload pictures, this abominable BinaryRead () method is very stupid, single file upload is no big deal, single if multiple pictures on the dedicated can spend a lot of strength ...! And now ASP.net will solve the previous ASP file upload a variety of problems, so that you in the ASP.net easily developed a powerful upload program, below you see examples.
First, create a database table of pictures stored in SQL Server, Sqlscript as follows:
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N) [dbo].[ Image] ") and OBJECTPROPERTY (ID, N" isusertable ") = 1)
drop table [dbo]. [Image]
Go
CREATE TABLE [dbo]. [Image] (
[IMG_PK] [INT] IDENTITY (1, 1) not NULL,
[Img_name] [varchar] (m) NULL,
[Img_data] [Image] Null
[Img_contenttype] [varchar] (m) NULL
) on [PRIMARY] textimage_on [PRIMARY]
Go
Codebehind file:
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 Study.uploadimage
{
<summary>
Summary description of the imgupload.
</summary>
public class Imgupload:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputText imgname;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}
Connection. Open ();
int numrowsaffected = command. ExecuteNonQuery ();
Connection. Close ();
return numrowsaffected;
}
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);
Codebehind file:
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.Data.SqlClient;
Namespace Study.uploadimage
{
<summary>
Summary description of the Imgview.
</summary>
public class Imgview:System.Web.UI.Page
{
int imgid = Int. Parse (request.querystring["id"]);
String sqltext = "Select Img_name, Img_data, img_contenttype from image where img_pk=" + imgid;
Trace.Write (SQLTEXT);
SqlCommand mycommand = new SqlCommand (SQLText, MYDSN);
SqlDataReader Dr =mycommand.executereader ();
if (Dr. Read ())
{
Response.ContentType = (dr["Img_contenttype"). ToString ());
Response.BinaryWrite ((byte[]) dr["Img_data");
}
Mydsn.close ();
}
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}
So this program is complete, simple. Of course there are a lot of improvements, I hope that we think more of the series can write more image upload program.
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.