zoukankan
html css js c++ java
Asp.Net在SqlServer中的图片存取
在使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
一,上传并存入sqlserver
数据库结构
create
table
test
{
id
identity
(
1
,
1
),
FImage
image
}
相关的存储过程
Create
proc
UpdateImage
(
@UpdateImage
Image
)
As
Insert
Into
test(FImage)
values
(
@UpdateImage
)
GO
在upphoto.aspx文件中添加如下:
<
input id
=
"UpPhoto" name
=
"UpPhoto" runat
=
"server" type
=
"
file
"
>
<
asp:Button id
=
"btnAdd" name
=
"btnAdd" runat
=
"server"
Text
=
"上传"
></
asp:Button
>
然后在后置代码文件upphoto.aspx.cs添加btnadd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//
获得图象并把图象转换为byte
[]
HttpPostedFile upPhoto
=
UpPhoto.PostedFile;
int
upPhotoLength
=
upPhoto.ContentLength;
byte
[]
PhotoArray
=
new Byte
[
upPhotoLength
]
;
Stream PhotoStream
=
upPhoto.InputStream;
PhotoStream.
Read
(PhotoArray,
0
,upPhotoLength);
//
连接数据库
SqlConnection conn
=
new SqlConnection();
conn.ConnectionString
=
"Data Source
=
localhost;
Database
=
test;
User
Id
=
sa;Pwd
=
sa";
SqlCommand cmd
=
new SqlCommand("UpdateImage",conn);
cmd.CommandType
=
CommandType.StoredProcedure;
cmd.Parameters.
Add
("
@UpdateImage
",SqlDbType.
Image
);
cmd.Parameters
[
"@UpdateImage"
]
.Value
=
PhotoArray;
//
如果你希望不使用存储过程来添加图片把上面四句代码改为:
//
string strSql
=
"
Insert
into
test(FImage)
values
(
@FImage
)";
//
SqlCommand cmd
=
new SqlCommand(strSql,conn);
//
cmd.Parameters.
Add
("
@FImage
",SqlDbType.
Image
);
//
cmd.Parameters
[
"@FImage"
]
.Value
=
PhotoArray;
conn.
open
();
cmd.ExecuteNonQuery();
conn.
Close
();
}
二,从sqlserver中读取并显示出来
在需要显示图片的地方添加如下代码:
<
asp:
image
id
=
"imgPhoto" runat
=
"server" ImageUrl
=
"ShowPhoto.aspx"
></
asp:
image
>
showphoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if
(!Page.IsPostBack)
{
SqlConnection conn
=
new SqlConnection()
conn.ConnectionString
=
"Data Source
=
localhost;
Database
=
test;
User
Id
=
sa;Pwd
=
sa";
string strSql
=
"
select
*
from
test
where
id
=
2
";
//
这里假设获取id为2的图片
SqlCommand cmd
=
new SqlCommand()
reader.
Read
();
Response.ContentType
=
"application
/
octet
-
stream";
Response.BinaryWrite((Byte
[]
)reader
[
"FImage"
]
);
Response.
End
();
reader.
Close
();
}
}
查看全文
相关阅读:
[MySQL] LIMIT 分页优化
[Flutter] 因为不讲这个重点, 全网 flutter 实战视频沦为二流课程
[Kafka] |FAIL|rdkafka#producer-1 : Receive failed: Disconnected
[Flutter] lib/main.dart:1: Warning: Interpreting this as package URI, 'package:flutter_app/main.dart'.
[Flutter] 写第一个 Flutter app,part1 要点
[Go] 开始试探一门新语言的五点思考
[转]Android进程间通信
[转]android ANR产生原因和解决办法
Android开发之旅(二)服务生命周期和广播接收者生命周期
为什么要有handler机制?handler机制的原理
原文地址:https://www.cnblogs.com/RuiLei/p/568741.html
最新文章
[FE] Chrome Extension 五步曲
使用 Nginx 阻止恶意 IP 访问
[FE] Give some magic ! 那些奇思妙想的 Chrome 插件
[Mobi] cordova requirements,Exception in thread "main" java.lang.NoClassDefFoundError
[INet] 借助 HTTP Cache 加速应用响应
[Mobi] 移动端应用技术选型的思考, Native, Flutter, Quasar, React Native
[PHP] Laravel 体现 MySQL、Sqlite 数据的大小写敏感
[PHP] 浅谈 Laravel Authentication 的 auth:api
MacbookPro15 2019 闪屏雪花现象方案汇总
[PHP] 浅谈 Laravel Authentication 的 guards 与 providers
热门文章
[K8s] Kubernetes 是什么 不是什么
[Py] 简单的 Python 运行环境
[Arch] 域名解析常用两步设置
[Swoole入门到进阶] [公开课] Swoole服务器-Server的四层生命周期
[Swoole入门到进阶] [公开课] Swoole协程-Swoole4.4.4 提供 WaitGroup 功能
[Docker] 六步运行一个 sentry 实例
[Go] 数据类型,变量与变量作用域,常量
[Go] 环境变量,模块化与基础语法
[Sw] Swoole 生态迷局,基于 Swoole 的第 109 框架
[分享会] 微服务框架设计 (基于Swoole)
Copyright © 2011-2022 走看看