C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体
//浏览图片
private void btnUp_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "选择要上传的图片";
ofd.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
ofd.ShowDialog();
textBox1.Text = ofd.FileName;
if (!File.Exists(ofd.FileName))
{
MessageBox.Show("照片为空");
return;
}
}
//上传保存到数据库
private void btnUpLoad_Click(object sender, EventArgs e)
{
string strPath = txtbImage.Text.Trim();
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
byte[] byteFile = new byte[fs.Length];
fs.Read(byteFile, , (int)fs.Length);
fs.Close();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strSql = "insert into test(FileName,Img) Values(@FileName,@Img)";
cmd.CommandText =strSql ;
//cmd.Parameters.AddWithValue("@FileName", strPath);
//cmd.Parameters.AddWithValue("@Img", byteFile);
//或者
SqlParameter[] parameters = new SqlParameter[];
parameters[] = new SqlParameter("@FileName", SqlDbType.NVarChar, );
parameters[].Value = strPath;
parameters[] = new SqlParameter("@Img", SqlDbType.Image,int.MaxValue);
parameters[].Value = byteFile;
cmd.Parameters.AddRange(parameters);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("上传成功");
}
catch
{
conn.Close();
MessageBox.Show("上传失败!");
}
}
从数据库读取图片显示到窗体:
//读到图片显示到PictureBox
private void btnDownLoad_Click(object sender, EventArgs e)
{
byte[] bytFile;
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
try
{
SqlCommand cmd = new SqlCommand();
string strSql = "select img from test where ID=3";
cmd.Connection = conn;
cmd.CommandText = strSql;
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
bytFile = (Byte[])sdr["Img"];
}
else
{
bytFile = new byte[];
}
sdr.Close();
conn.Close();
//通过内存流MemoryStream,
//把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,
//让数据库中的图片直接显示在窗体上。
MemoryStream ms = new MemoryStream(bytFile, , bytFile.Length);
this.picImage.Image = Image.FromStream(ms);
//关闭内存流
ms.Close();
}
catch
{
conn.Close();
MessageBox.Show("失败");
}
}
代码转自IT学习广场http://www.itxxgc.com/net/detail/30
来自凌波小屋-----冯和超的笔记-------
C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体的更多相关文章
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
下面是两段关键代码: /// <summary> /// 将一张图片转换为字节 /// </summary> /// <param name="img" ...
- MySQL数据库写入图片并读取图片显示到JLabel上的详解
相较于Oracle,MySQL作为一个轻量级的开源的数据库,可谓是大大简化了我们的操作.这次我就来写一个关于数据库存入图片,获取图片的例子吧,也为了今后的复习使用.(我们一般采取存入路径的方式,而不是 ...
- VS.C#如何向数据数据库中存入和读取图片的
写入图片部分代码:假设图片为 test.gifbyte [] bytes = File.ReadAllBytes(@"c:\test.gif");SqlConnection con ...
- [转]asp.net mvc 从数据库中读取图片
本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html 首先是创建一个类,继承于ActionResult,记住要引用Syste ...
- asp.net mvc 从数据库中读取图片的实现代码
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...
- php实现上传图片保存到数据库的方法
http://www.jb51.net/article/61034.htm 作者:傲雪星枫 字体:[增加 减小] 类型:转载 这篇文章主要介绍了php实现上传图片保存到数据库的方法,可通过将图片保 ...
- C# 图片保存到数据库和从数据库读取图片并显示
图片保存到数据库的方法: public void imgToDB(string sql) { //参数sql中要求保存的imge变量名称为@images //调 ...
- 基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作
由于一个客户朋友的需求,需要我的Winform开发框架支持国产达梦数据库的操作,这个数据库很早就听过,但是真正一般项目用的很少,一般在一些特殊的项目可能需要用到.由于我的Winform开发框架,是基于 ...
- winform里dataGridView分页代码,access数据库
winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...
随机推荐
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- mysql 树形数据,层级数据Managing Hierarchical Data in MySQL
原文:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 引言 大多数用户都曾在数据库中处理过分层数据(hiera ...
- Unity NGUI Tween的用法
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://www.colabug.com/thread-1029974-1-1.html,作者:COLABUG.COM 橘虞 htt ...
- 【转】Android低功耗蓝牙应用开发获取的服务UUID
原文网址:http://blog.csdn.net/zhangjs0322/article/details/39048939 Android低功耗蓝牙应用程序开始时获取到的蓝牙血压计所有服务的UUID ...
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 使用StaticResource给控件定义公共的样式和属性来写界面XAML
一:效果图 二:定义公共的样式和属性 在MainPage.xaml中 <phone:PhoneApplicationPage.Resources> <SolidColorBrush ...
- Android按钮式进度条
package com.example.progress.demo; import android.annotation.SuppressLint; import android.content.Co ...
- 课后习题 SPJ
CREATE TABLE S ( SNO char(2) primary key, SNAME varchar(20), STATUS char(4), CITY char(10) ); create ...
- Python文件中文编码问题
读写中文 需要读取utf-8编码的中文文件,先利用sublime text软件将它改成无DOM的编码,并且在第一行写: # encoding: utf-8 然后用以下代码: with codecs.o ...
- HttpURLConnection学习
转自:http://mobile.51cto.com/abased-448264.htm 最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给 ...