网络软件 系统工具 应用软件 图形图像 多媒体类 免费游戏 安全相关 免费音乐 网页素材 电子书籍 考试考题 建站源码
教育教学 多媒体类 编程开发 操作系统 游戏天地 娱乐天地 简历求职 站长专区 网页设计 安全技术 图形图像 文学驿站
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网页设计 | 技术开发 | 专栏
当前位置:热点网络学院技术开发.Net 专栏数据库应用图片保存到数据库和从数据库读取图片并显示(c#)
精品推荐
热点TOP10
·VB.NET的数据库基础编程(1)
·VB.NET的数据库基础编程(2)
·oracle中对db的不同命名方式
·ADO连接数据库字符串大全
·用VB.NET来做个性化浏览器
·QQ聊天记录器演示程序(一)
·玩转Windows桌面图标
·ACCESS在Web.config里设置连接字符串
·还原数据库的经典做法
·如何在GridView中一次性批量更新多行数据
·DataSet 添加数据集、行、列、主键和外键等操作示例
·ASP.NET中数据库的操作初步----DataSet操作数据库
·运用每个开发者都必须了解的十个安全技巧来保护代码
·asp.net连接Access数据库
·远程连接access数据库的方法
·asp.net关于文件在数据库的存入和读取
·将Asp.net从VS2003以及VS2002工程转换为VS2005工程的简单而又行之有效的方法
·如何于DataGridView控件中以跨数据行方式显示数据
·101个微软提供的Visual Studio 2005示例
·将上传图片打上防伪图片水印并写入数据库
图片保存到数据库和从数据库读取图片并显示(c#)
日期:2007年1月13日 作者: 查看:[大字体 中字体 小字体]

        public void imgToDB(string sql)
        {   //参数sql中要求保存的imge变量名称为@images
            //调用方法如:imgToDB("update UserPhoto set Photo=@images where UserNo='" + temp + "'");
            FileStream fs = File.OpenRead(t_photo.Text);
            byte[] imageb = new byte[fs.Length];
            fs.Read(imageb, 0, imageb.Length);
            fs.Close();
            SqlCommand com3 = new SqlCommand (sql,con);
            com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;
            if (com3.Connection.State == ConnectionState.Closed)
                com3.Connection.Open();
            try
            {
                com3.ExecuteNonQuery();
            }
            catch
            { }
            finally
            { com3.Connection.Close(); }
        }

数据库中读出图片并显示在picturebox中:

方法一:
private void ShowImage(string sql)
     {
     //调用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'");
     SqlCommand cmd = new SqlCommand(sql, conn);
     conn.Open();
     byte[] b= (byte[])cmd.ExecuteScalar();
     if (b.Length 〉 0)
     {
     MemoryStream stream = new MemoryStream(b, true);
     stream.Write(b, 0, b.Length);
     pictureBox1.Image = new Bitmap(stream);
     stream.Close();
     }
     conn.Close();
     }

方法二:当在dg中选中某行时:
  private void dg_MouseUp(object sender, MouseEventArgs e)
        {
            //整行选择
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {//用户编号,姓名,性别,身份证号,籍贯,学院,系所,校区,部门,电话,照片
                 //显示相片
                object imgobj=dg[10, dg.CurrentRow.Index].Value;
                if (imgobj != null && !Convert.IsDBNull(imgobj))
                {
                    byte[] imgb = (byte[])imgobj;
                    MemoryStream memStream = new MemoryStream(imgb);
                    try
                    {
                        Bitmap myimge = new Bitmap(memStream);
                        this.pictureBox1.Image = myimge;
                    }
                    catch
                    {
                        DB.msgbox("从数据库读取相片失败!");
                    }
                }
                else
                    pictureBox1.Image = null;
            }
        }

http://www.cnblogs.com/tuyile006/archive/2007/01/08/614718.html

(出处:http://www.xmsc.com.cn)

关于我们 | 帮助(?) | 版权声明 | 友情连接 
Copyright 2005-2005 xmsc.com.cn All Rights Reserved.
Powered by:mesky