CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

如何在sql server如何保存和提取图片

楼主zhbzspqxl()2005-06-02 08:24:58 在 .NET技术 / C# 提问

现有在sql   server中有表student,字段为studentid(int),Name(varchar),   photo(image),请问如何在表中保存和提取图片等。老师要求保存和提取该数据表中的信息(包括图片)全部用存储过程来实现,在WINFORM下开发,C/S形式,高手帮们给段代码吧!小弟初学C#,给段详细代码过程小弟拜谢了!!!!  
  问题点数:0、回复次数:7Top

1 楼drk928(一起看斜阳)回复于 2005-06-02 08:55:07 得分 0

上传图片  
   
    If   OpenFileDialog1.ShowDialog()   =   DialogResult.Cancel   Then  
  Exit   Sub  
  End   If  
  If   OpenFileDialog1.ShowDialog()   =   DialogResult.OK   Then  
  With   PictureBox1  
  .Image   =   Image.FromFile(OpenFileDialog1.FileName)  
  .SizeMode   =   PictureBoxSizeMode.CenterImage  
  .BorderStyle   =   BorderStyle.Fixed3D  
  End   With  
  End   If  
   
  Dim   ms   As   New   MemoryStream()  
  PictureBox1.Image.Save(ms,   PictureBox1.Image.RawFormat)  
  Dim   arrImage()   As   Byte   =   ms.GetBuffer  
  ms.Close()  
  Dim   imgid   As   String   =   CInt(Int(10   *   Rnd())   +   Int(100   *   Rnd())   +   Int(1000   *   Rnd())   +   Int(10000   *   Rnd())   +   Int(100000   *   Rnd())   +   Int(1000000   *   Rnd())   +   Int(10000000   *   Rnd())).ToString  
  Try  
  Dim   conn   As   New   GWConnection()  
  Dim   northwindConnection   As   New   SqlConnection()  
  northwindConnection   =   conn.GetCN()  
   
  Dim   strSQL   As   String   =   "INSERT   INTO   t2_005(imgid,pactcode,Picture)   VALUES(@imgid,@pactcode,@Picture)"  
  Dim   cmd   As   New   SqlCommand(strSQL,   northwindConnection)  
  With   cmd  
  .Parameters.Add(New   SqlParameter("@imgid",   SqlDbType.NVarChar,   50)).Value   =   imgid  
  .Parameters.Add(New   SqlParameter("@pactcode",   SqlDbType.NVarChar,   16)).Value   =   txt_pactcode.Text  
  .Parameters.Add(New   SqlParameter("@Picture",   SqlDbType.Image)).Value   =   arrImage  
  End   With  
  If   Not   northwindConnection.State   =   ConnectionState.Open   Then  
  northwindConnection.Open()  
  End   If  
  cmd.ExecuteNonQuery()  
  northwindConnection.Close()  
  'txt_imgid.Text   =   imgid  
  Catch   ex   As   SqlException  
  MsgBox("图片上传失败!",   MsgBoxStyle.OKOnly   +   MsgBoxStyle.Exclamation,   "出错提示!")  
  End   Try  
     
  Top

2 楼bobseadream(bobo)回复于 2005-06-02 08:59:17 得分 0

//   Source   Code   for   Save   the   image   file   into   the   database  
   
  public   void   OnUpload(Object   sender,   EventArgs   e)  
  {  
          //   Create   a   byte[]   from   the   input   file  
          int   len   =   Upload.PostedFile.ContentLength;  
          byte[]   pic   =   new   byte[len];  
          Upload.PostedFile.InputStream.Read   (pic,   0,   len);  
          //   Insert   the   image   and   comment   into   the   database  
          SqlConnection   connection   =   new    
              SqlConnection   (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");  
          try  
          {  
                  connection.Open   ();  
                  SqlCommand   cmd   =   new   SqlCommand   ("insert   into   Image   "    
                      +   "(Picture,   Comment)   values   (@pic,   @text)",   connection);  
                  cmd.Parameters.Add   ("@pic",   pic);  
                  cmd.Parameters.Add   ("@text",   Comment.Text);  
                  cmd.ExecuteNonQuery   ();  
          }  
          finally    
          {  
                  connection.Close   ();  
          }  
  }  
   
  private   void   GetImage(string   str)  
  {  
  int   num=this.dataGrid1.CurrentRowIndex;  
  if(num==-1)  
  {  
  return;  
  }  
  if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull)  
  {  
  byte[]   bytes=(byte[])dataset.Tables[0].Rows[num]["照片"];  
  MemoryStream   memStream=new   MemoryStream(bytes);  
  Bitmap   bitmap   =   new   Bitmap(memStream);  
  memStream.Close();  
  this.pictureBox1.Image   =   bitmap;  
  }  
  else  
  {  
  this.pictureBox1.Image   =   null;  
  }  
  }  
  Top

3 楼dan83(共想主义事业)回复于 2005-06-02 09:16:03 得分 0

学习ingTop

4 楼wang8712(Andy)回复于 2005-06-02 10:25:42 得分 0

你获取图片可以在数据库中保存数据库的路径,在程序中取就方便了.Top

5 楼zhbzspqxl()回复于 2005-06-03 07:54:44 得分 0

老师要求数据库中保存图片,我用的是C#,如何做呀!!!!Top

6 楼lyb_abiandbel(专注于OO分析与设计)回复于 2005-06-03 08:34:39 得分 0

添加图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506  
   
  显示图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1  
   
  我用的就是这个,好用的。  
   
  Top

7 楼NetFishDuDu()回复于 2005-06-03 08:53:55 得分 0

搜索一下,有很多这样的例子,可能语言不同,但思路是一样的吧Top

相关问题

  • 怎样保存图象到sql server中
  • SQL Server保存空格的问题
  • 如何保存触发器(SQL Server 2000)
  • ADO中提取数据库中的表名称(SQL Server)
  • jb6:SQL SERVER 中记录的提取,复制问题?
  • 如何从SQL SERVER 中提取指定格式的时间 ?
  • 用vb的ado向sql server 保存记录出错。
  • 怎么样保存SQL server中blob型字段的值
  • 如何将WORD文档保存在数据库中(SQL SERVER)
  • 新手问:在sql server中怎样保存多个数组

关键词

  • c#
  • rnd
  • northwindconnection
  • 图片
  • imgid
  • openfiledialog
  • picturebox
  • 保存和提取
  • dim
  • aspx

得分解答快速导航

  • 帖主:zhbzspqxl

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo