利用OpenFileDialog 获取图片存储到数据库中
private void button1_Click(object sender, EventArgs e)
{
string fName;
OpenFileDialog openFileDialog = new OpenFileDialog();//实例化
openFileDialog.InitialDirectory = "e:\\141\\";//打开的默认路径
openFileDialog.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog.FileName;
//textBox1.Text = File.ReadAllText(fName);
FileStream fs=new FileStream (fName ,FileMode.Open );
byte [] imgbt=new byte [fs .Length ];
BinaryReader br = new BinaryReader(fs);
imgbt = br.ReadBytes(Convert.ToInt32(fs.Length));
string cnnstr = "server=.;User ID=sa;Password=admin;Database=student";
SqlConnection conn = new SqlConnection(cnnstr);
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string sql = "insert into a values('01',@image)";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Parameters.Add("image", SqlDbType .Image , imgbt.Length);
comm.Parameters[0].Value = imgbt;
comm.ExecuteNonQuery();
conn.Close();
}
再读取出来
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
if (dr["imagetest"] != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dr["imagetest"]);//把照片读到MemoryStream里
Image imageBlob = Image.FromStream(ms, true);//用流创建Image
pictureBox1.Image = imageBlob;//输出图片
}
else//照片字段里没值,清空pb
{
pictureBox1.Image = null;
}
}
利用OpenFileDialog 获取图片存储到数据库中的更多相关文章
- Java中获取刚插入数据库中的数据Id(主键,自动增长)
public int insert(String cName, String ebrand, String cGender) { String sql = "insert into Clot ...
- 利用SQL语句查找某数据库中所有存储过程包含的内容(转)
Use 数据库DECLARE @ProcName varchar(50)Create Table #tmpName(Content varchar(2000))Create Table #tmp(P ...
- 利用Jquery获取、设置iframe中元素
<iframe id="iframe" src="'+url+'"></iframe>'; //iframe加载完成后 $(" ...
- 获取SQL Server数据库中的表和字段描述
获取所有dbo表的扩展属性: SELECT * FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', default, NULL, ...
- 利用SQL语句查询一个数据库中的所有表
SQL : select * from information_schema.tables ORACLE: select table_name from user_tables ACCESS: s ...
- 如何简单地利用Bitmap为中介储存图片到数据库中
这是我的第一篇博文,请大家多多指教! 大概一个月之前,在跟朋友合作开发一个APP的过程中,我们发现到一个问题:图片的存储.因为数据库没有图片这种数据类型,当用户上传的图片需要存储的时候 ...
- MVC模式:实现数据库中数据的增删改查功能
*.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...
- MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)
设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...
- Sql server 数据库中,纯SQL语句查询、执行 单引号问题。
在默认值情况下, select 'abc',Titile from tb_Name; ---输出内容 是abc: 如果想输出 单引号 'abc,需要使用select '''abc',Titile f ...
随机推荐
- 异步编程与scrapy
https://python-parallel-programmning-cookbook.readthedocs.io/zh_CN/latest/chapter1/index.html https: ...
- Openstack组件部署 — Keystone功能介绍与认证实现流程
目录 目录 前文列表 Keystone认证服务 Keystone认证服务中的概念 Keystone的验证过程 简单来说 前文列表 Openstack组件部署 - Overview和前期环境准备 Ope ...
- python模块学习之testlink (自动回写测试案例执行结果到testlink)
安装 pip install TestLink-API-Python-client #!/usr/bin/env Python # -*- coding: utf-8 -*- ''' Created ...
- jupyter notebook的魔法命令 % %%
Magic单元分为两种,一种是line magics,另外一种cell magics. Line magic是通过在前面加%,表示magic只在本行有效. Cell magic是通过在前面加%%,表示 ...
- centos7 安装python虚拟环境
本篇主要介绍centos7系统下,安装python3虚拟环境.环境:系统centos7,源代码安装python3,/usr/bin/python3为自己安装的. 安装支持包 yum install p ...
- css 深入理解
场景一.边框半透明,背景绿色 默认情况下背景会延伸到边框所在的下边 css2 中我们只能接受 css3 中我们可以通过 background-clip 属性来实现 border: 10px soli ...
- MES training
unique identity 1.project name , namespace 2. select XML (not html) 3. view and controller will be i ...
- Spark使用Java读取mysql数据和保存数据到mysql
原文引自:http://blog.csdn.net/fengzhimohan/article/details/78471952 项目应用需要利用Spark读取mysql数据进行数据分析,然后将分析结果 ...
- CXF异常:No operation was found with the name
https://blog.csdn.net/qq_18675693/article/details/52134805 不同包下面,别忘了namespace最后要加“/”
- 2019-8-31-How-to-use-code-to-exit-the-application-in-UWP
title author date CreateTime categories How to use code to exit the application in UWP lindexi 2019- ...