zoukankan
html css js c++ java
保存/读取图片到数据库
private
void
SaveImage(
string
fileName)
{
//
Read the file into a byte array
using
(FileStream fs
=
new
FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte
[] imageData
=
new
Byte[fs.Length];
fs.Read(imageData,
0
, (
int
)fs.Length);
using
(SqlConnection conn
=
new
SqlConnection(connectionString))
{
string
sql
=
"
insert into image (imagefilename,blobdata) values (@filename,@blobdata)
"
;
SqlCommand cmd
=
new
SqlCommand(sql, conn);
cmd.Parameters.Add(
"
@filename
"
,SqlDbType.Text);
cmd.Parameters[
"
@filename
"
].Direction
=
ParameterDirection.Input;
cmd.Parameters.Add(
"
@blobdata
"
, SqlDbType.Image);
cmd.Parameters[
"
@blobdata
"
].Direction
=
ParameterDirection.Input;
//
Store the byte array within the image field
cmd.Parameters[
"
@filename
"
].Value
=
fileName;
cmd.Parameters[
"
@blobdata
"
].Value
=
imageData;
conn.Open();
if
(cmd.ExecuteNonQuery()
==
1
)
{
MessageBox.Show(
"
Done
"
);
}
}
}
}
private
void
LoadImage(
string
fileName)
{
using
(SqlConnection conn
=
new
SqlConnection(connectionString))
{
string
sql
=
"
select blobdata from Image where ImageFileName like @filename
"
;
SqlCommand cmd
=
new
SqlCommand(sql, conn);
cmd.Parameters.Add(
"
@filename
"
, SqlDbType.Text);
cmd.Parameters[
"
@filename
"
].Value
=
fileName;
conn.Open();
object
objImage
=
cmd.ExecuteScalar();
byte
[] buffer
=
(
byte
[])objImage;
BinaryWriter bw
=
new
BinaryWriter(
new
FileStream(
"
C:\\abcd.png
"
, FileMode.Create));
bw.Write(buffer);
bw.Close();
MemoryStream ms
=
new
MemoryStream(buffer);
Image bgImage
=
Image.FromStream(ms);
ms.Close();
this
.BackgroundImage
=
bgImage;
}
}
查看全文
相关阅读:
函数进阶,递归,二分法查找
内置函数
IDEA使用maven创建web工程并完善的过程
后端传入前端的数据的属性名全部为小写的解决方法
今日总结,复习了很多知识
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ztreeoneServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; 错误的解决方法
xxx cannot be resolved to a type 的可能的解决方法,mybatis的Example类不存在
记录一下Spirng Initializr初始化项目的时候pom文件的内容
使用nacos进行服务注册的配置
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing问题的一种解决方法参考
原文地址:https://www.cnblogs.com/sskset/p/591771.html
最新文章
hdu2899Strange fuction(解方程+二分)
hdu2199Can you solve this equation?(解方程+二分)
cf#516C. Oh Those Palindromes(最多回文子串的字符串排列方式,字典序)
cf#516B. Equations of Mathematical Magic(二进制,位运算)
cf#516A. Make a triangle!(三角形)
hdu1312Red and Black(迷宫dfs,一遍)
hdu1010Tempter of the Bone(迷宫dfs+剪枝)
Django命名空间app_name和namespace
简单HTML属性、CSS样式及文件的命名规则
关于《python基础教程》字符串章节例子的多个花括号问题
热门文章
博客园如何转载别人的文章
pycharm里的jinja2注释问题
助力函数式编程
字符编码
Python面试题(摘自武沛齐的博客)
Python内置常用模块
Python多进程与多线程
os模块之os.path,序列化模块
常用模块之re模块以及正则表达式扩展
二分法查找,冒泡排序,递归函数
Copyright © 2011-2022 走看看