1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
using System.IO;
13
14
namespace MyTest
15
{
16
/// <summary>
17
/// WebForm1 的摘要说明。
18
/// </summary>
19
public class WebForm1 : System.Web.UI.Page
20
{
21
protected System.Web.UI.WebControls.TextBox TextBox1;
22
protected System.Web.UI.WebControls.Button SubmitButton;
23
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;
24
protected System.Web.UI.WebControls.Button butDisplay;
25
protected System.Web.UI.WebControls.TextBox txtPicID;
26
protected System.Web.UI.WebControls.Label lblMessage;
27
string ConnStr = "server=localhost;uid=sa;pwd=;database=MyTest";
28
29
30
private void Page_Load(object sender, System.EventArgs e)
31
{
32
33
}
34
35
Web 窗体设计器生成的代码
57
58
//存入数据库
59
private void SubmitButton_Click(object sender, System.EventArgs e)
60
{
61
//HttpPostedFile对象,用于读取图像文件属性
62
HttpPostedFile UpFile = UP_FILE.PostedFile;
63
//FileLength变量存储图片的字节大小
64
int FileLength = UpFile.ContentLength;
65
66
try
67
{
68
if(FileLength == 0)
69
{
70
this.lblMessage.Text = "您未选择上传的文件";
71
}
72
else
73
{
74
//创建存储图片文件的临时Byte数组
75
byte[] FileByleArray = new byte[FileLength];
76
//建立数据流对象
77
Stream streamObject = UpFile.InputStream;
78
//读取图像文件数据
79
//FileByleArray为数据储存体,0为数据指针位置、FileLength为数据长度
80
streamObject.Read(FileByleArray,0,FileLength);
81
//数据库操作
82
string query = "INSERT INTO Picture(PicData,PicType,PicDescription,PicSize) VALUES (@PicData,@PicType,@PicDescription,@PicSize)";
83
SqlCommand com = new SqlCommand(query,new SqlConnection(ConnStr));
84
85
//添加各项参数并赋值
86
com.Parameters.Add("@PicData",SqlDbType.Image);
87
com.Parameters.Add("@PicType",SqlDbType.VarChar,50);
88
com.Parameters.Add("@PicDescription",SqlDbType.VarChar,200);
89
com.Parameters.Add("@PicSize",SqlDbType.BigInt);
90
com.Parameters["@PicData"].Value = FileByleArray;
91
com.Parameters["@PicType"].Value = UpFile.ContentType;
92
com.Parameters["@PicDescription"].Value = this.TextBox1.Text;
93
com.Parameters["@PicSize"].Value = FileLength;
94
95
//执行数据库操作
96
com.Connection.Open();
97
com.ExecuteNonQuery();
98
com.Connection.Close();
99
//提示上传成功
100
this.lblMessage.Text = "上传成功!";
101
}
102
}
103
catch(Exception er)
104
{
105
this.lblMessage.Text = er.Message.ToString();
106
}
107
}
108
109
//读出图片并显示
110
private void butDisplay_Click(object sender, System.EventArgs e)
111
{
112
//获取输入的图片ID
113
int ImgID = int.Parse(this.txtPicID.Text);
114
//创建数据库连接字符串和SQL语句
115
string query = "SELECT * FROM Picture WHERE ID=@ImgID";
116
//创建SqlCommand对象并对参数进行初始化赋值
117
SqlCommand com = new SqlCommand(query,new SqlConnection(ConnStr));
118
com.Parameters.Add("@ImgID",SqlDbType.BigInt);
119
com.Parameters["@ImgID"].Value = ImgID;
120
//打开数据库连接
121
com.Connection.Open();
122
SqlDataReader dr = com.ExecuteReader();
123
124
if(dr.Read())
125
{
126
Response.ContentType = dr["PicType"].ToString();
127
Response.OutputStream.Write((byte[])dr["PicData"],0,int.Parse(dr["PicSize"].ToString())+50000);
128
}
129
else
130
{
131
this.lblMessage.Text = "没有这个图片的ID号";
132
Response.End();
133
}
134
135
//关闭SqlDataReader对象和数据库连接
136
dr.Close();
137
com.Connection.Close();
138
139
}
140
141
142
}
143
}
144
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11
using System.Data.SqlClient;12
using System.IO;13

14
namespace MyTest15
{16
/// <summary>17
/// WebForm1 的摘要说明。18
/// </summary>19
public class WebForm1 : System.Web.UI.Page20
{21
protected System.Web.UI.WebControls.TextBox TextBox1;22
protected System.Web.UI.WebControls.Button SubmitButton;23
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;24
protected System.Web.UI.WebControls.Button butDisplay;25
protected System.Web.UI.WebControls.TextBox txtPicID;26
protected System.Web.UI.WebControls.Label lblMessage;27
string ConnStr = "server=localhost;uid=sa;pwd=;database=MyTest";28
29
30
private void Page_Load(object sender, System.EventArgs e)31
{32
33
}34

35
Web 窗体设计器生成的代码57

58
//存入数据库59
private void SubmitButton_Click(object sender, System.EventArgs e)60
{61
//HttpPostedFile对象,用于读取图像文件属性62
HttpPostedFile UpFile = UP_FILE.PostedFile;63
//FileLength变量存储图片的字节大小64
int FileLength = UpFile.ContentLength;65

66
try67
{68
if(FileLength == 0)69
{70
this.lblMessage.Text = "您未选择上传的文件";71
}72
else73
{74
//创建存储图片文件的临时Byte数组75
byte[] FileByleArray = new byte[FileLength];76
//建立数据流对象77
Stream streamObject = UpFile.InputStream;78
//读取图像文件数据79
//FileByleArray为数据储存体,0为数据指针位置、FileLength为数据长度80
streamObject.Read(FileByleArray,0,FileLength);81
//数据库操作 82
string query = "INSERT INTO Picture(PicData,PicType,PicDescription,PicSize) VALUES (@PicData,@PicType,@PicDescription,@PicSize)";83
SqlCommand com = new SqlCommand(query,new SqlConnection(ConnStr));84

85
//添加各项参数并赋值86
com.Parameters.Add("@PicData",SqlDbType.Image);87
com.Parameters.Add("@PicType",SqlDbType.VarChar,50);88
com.Parameters.Add("@PicDescription",SqlDbType.VarChar,200);89
com.Parameters.Add("@PicSize",SqlDbType.BigInt);90
com.Parameters["@PicData"].Value = FileByleArray;91
com.Parameters["@PicType"].Value = UpFile.ContentType;92
com.Parameters["@PicDescription"].Value = this.TextBox1.Text;93
com.Parameters["@PicSize"].Value = FileLength;94

95
//执行数据库操作96
com.Connection.Open();97
com.ExecuteNonQuery();98
com.Connection.Close();99
//提示上传成功100
this.lblMessage.Text = "上传成功!";101
}102
}103
catch(Exception er)104
{105
this.lblMessage.Text = er.Message.ToString();106
}107
}108

109
//读出图片并显示110
private void butDisplay_Click(object sender, System.EventArgs e)111
{112
//获取输入的图片ID113
int ImgID = int.Parse(this.txtPicID.Text);114
//创建数据库连接字符串和SQL语句115
string query = "SELECT * FROM Picture WHERE ID=@ImgID";116
//创建SqlCommand对象并对参数进行初始化赋值117
SqlCommand com = new SqlCommand(query,new SqlConnection(ConnStr));118
com.Parameters.Add("@ImgID",SqlDbType.BigInt);119
com.Parameters["@ImgID"].Value = ImgID;120
//打开数据库连接121
com.Connection.Open();122
SqlDataReader dr = com.ExecuteReader();123

124
if(dr.Read())125
{126
Response.ContentType = dr["PicType"].ToString();127
Response.OutputStream.Write((byte[])dr["PicData"],0,int.Parse(dr["PicSize"].ToString())+50000);128
}129
else130
{131
this.lblMessage.Text = "没有这个图片的ID号";132
Response.End();133
}134

135
//关闭SqlDataReader对象和数据库连接136
dr.Close();137
com.Connection.Close();138
139
}140

141
142
}143
}144


