保存图片到SQL Server
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
35
Web 窗体设计器生成的代码
57![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
135
//关闭SqlDataReader对象和数据库连接
136
dr.Close();
137
com.Connection.Close();
138
139
}
140![](/Images/OutliningIndicators/InBlock.gif)
141
142
}
143
}
144![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
126
![](/Images/OutliningIndicators/InBlock.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
139
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/InBlock.gif)
142
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
143
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
144
![](/Images/OutliningIndicators/None.gif)