C#Winform在数据库中读取图片文件和上传数据文件
来源:互联网 发布:nba2k16捏脸数据游侠网 编辑:程序博客网 时间:2024/05/18 08:01
读取数据库图片字段方法:
protected void LoadPicture() { Image img = null; if (this.gl_studentid.Length==0) { img = Image.FromFile("NoPicture.png");//如果数据库中没有图片则加载NoPicture.png这张图片 this.pictureBox1.Image = img; return; } string sql = "select * from student where studentid='" + gl_studentid + "'"; DataTable dt = DBUtil.GetData(sql); if (dt.Rows.Count>0) { try //用try 做判断 ,因为转化过程可能出错 { byte[] bytes = (byte[])dt.Rows[0]["img"]; MemoryStream ms = new MemoryStream(bytes); //核心方法 将图片加载到内存流中 this.pictureBox1.Image = Image.FromStream(ms); } catch (Exception) { img = Image.FromFile("NoPicture.png"); this.pictureBox1.Image=img; } } }向数据库中上传图片文件方法:(这里方便采用修改数据库的图片文件,原理相同)
private void btnSubmit_Click(object sender, EventArgs e) { string filePath = this.txtFilePath.Text.ToString(); if (filePath.Length==0) { MessageBox.Show("请选择图片!"); return; } if (gl_studentid.Length==0) { MessageBox.Show("请选择要修改的记录!"); } string sql = "update student set img=@img where studentid=@studentid"; SqlParameter p1 = new SqlParameter("@img", SqlDbType.Image); FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] bytes = new byte[fs.Length]; fs.Seek(0, SeekOrigin.Begin); //定义开始读文件的位置,为0位置 SeekOrigin.Begin意为设置0为一个开始标志位 fs.Read(bytes, 0, (int)fs.Length); p1.Value = bytes; SqlParameter p2 = new SqlParameter("@studentid", gl_studentid); using (SqlConnection conn=new SqlConnection(sqlconn)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.ExecuteNonQuery(); fs.Close(); fs.Dispose(); MessageBox.Show("图片修改成功!"); this.LoadPicture(); } }
0 0
- C#Winform在数据库中读取图片文件和上传数据文件
- C#winform在DataGridView中操作文件保存到数据库与从数据库中读取
- C#Winform 在DataGridView(SQL Server)中下载和上传文件方法
- c#winform中上传图片保存
- 上传图片到数据库,读取数据库中图片并显示(C#)
- 转:上传图片到数据库,读取数据库中图片并显示(C#)
- C文件操作;数据文件读取
- 从数据库中读取图片文件和将图片和写入到数据库中
- C# winform 上传文件、图片
- winform上传,读取图片绑定到datagridview
- 在Java中使用JDBC向数据库插入图片和读取数据库中的图片(八)
- struts2 文件上传(指定上传图片,单文件上传和批量上传放在一个jsp和Action中)
- VS 2005中winForm开发(C#)—图片上传到数据库与显示(sql server 2005)
- Winform 保存和读取图片
- 读取两个excl文件和图片文件夹 并将其中的图片文件移动其他文件夹excl内容上传数据库
- HTML中上传与读取图片或文件(input file)----在路上(25)
- C#Winform读取txt文件
- C#winform读取.txt文件
- 建造者模式
- 快速排序算法
- swift基本语法(总结提炼版)之008 swift 之字典
- 双按钮双事件监听机制的简单java GUI
- UIAutomator定位Android控件的方法实践和建议
- C#Winform在数据库中读取图片文件和上传数据文件
- Android中ScrollView嵌套ListView
- 静态代理模式
- swift基本语法(总结提炼版)之009 swift 之字符串
- 1047. 编程团体赛
- 进程管理
- db link的使用
- 安全卫士第一天笔记
- Mysql安装