Linux中国  设为主页
 收藏本站
 
当前位置: > 首页 ->编程语言 ->ASP.NET ->图片保存到数据库和从数据库读取图片并显示(c#)
  相关分类: 
ASP
ViualBasic
UML / Rational Rose
PHP4/PHP5
Perl
JAVA/JSP教程
Delphi
ColdFusion
CGI
C/C++
ASP.NET
XML
  站内搜索: 
热门文章排行
热门文章排行 C#编写的windows计算器-源代码(06-04)
C# 操作文件(06-04)
c#操作word表格(06-04)
使用C#在进度条中显示复制文件的进度(06-04)
C# GridView 排序及分页(06-04)
精采文章排行
精采文章排行 用C#实现FTP搜索引擎(06-04)
对C# 2.0中匿名方法的怀疑分析(06-04)
在C#中利用DirectX实现声音播放(06-04)
使用C#在进度条中显示复制文件的进度(06-04)
C#中利用Markup Service实现HTML解析(06-04)
  ·对C# 2.0中匿名方法的怀疑分析·在C#中利用DirectX实现声音播放·使用C#在进度条中显示复制文件的进度·C#中利用Markup Service实现HTML解析为DO·c#操作word表格·C#代码操作IIS之虚拟目录·如何使用C#在发送往client的内容上加js代·[C#]解决读写包含汉字的txt文件时乱码的·C#实现根据域名查询ip实例

图片保存到数据库和从数据库读取图片并显示(c#)

作者:Webmaster   来源:Linuxdby.com   点击:   日期:2007-06-04 [收藏] [投稿]

  IE是否经常中毒?推荐您

        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
                    {

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<

上一页12 下一页

上一篇:通过COM来获取CookieContainer,简单又好用   下一篇:C#入门代码
文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论

   相关文章:
·用C#实现FTP搜索引擎

   文章评论:(1条)
  
 请留名: 匿名评论   点击查看所有评论 论坛讨论
 

 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。