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

sqlserver 2000中图片的存储与读取操作?

楼主12345521(上山打老虎)2004-09-01 08:37:38 在 .NET技术 / C# 提问

谁有用C#   sqlserver2000   webForm实现的存储图片与读取?谢谢 问题点数:0、回复次数:7Top

1 楼qingyun1020(星期零)回复于 2004-09-01 08:40:51 得分 0

我有Top

2 楼lhx1977(清水无鱼)回复于 2004-09-01 08:41:16 得分 0

操作都是一样的  
  在这里搜索一下吧.我就是在这里搜索到的.  
  Top

3 楼CMIC(大象)回复于 2004-09-01 08:45:49 得分 0

http://dev.csdn.net/develop/article/12/12172.shtmTop

4 楼91bct(Jerry)回复于 2004-09-01 10:43:25 得分 0

这是下载的一段资料,供参考:  
  读取图片:  
  I   thought   it   would   be   fun   to   write   a   simple   program   that   displayed   flashcards   from   a   database   and   at   the   same   time,   show   you   how   to    
  read   and   write   images   to   the   database.     This   program   is   a   simple   flashcard   program   that   talks   to   a   single   table   in   a   Sql   Server    
  Database.     The   design   of   the   database   table   is   shown   below:    
     
  Figure   2   -   FlashCard   DB   Design   Reverse   Engineered   Using   WithClass   2000    
  The   Picture   field   is   of   type   varbinary.     As   seen   from   the   database   design   diagram,   it   holds   8000   bytes   of   data,   enough   for   a   square    
  picture   of   about   89   x   89   pixels.     When   I   created   the   table   using   the   Server   Explorer   wizard,   I   needed   to   specifiy   the   maximum   length    
  in   order   to   get   the   image   storage   to   work   correctly.     Although   the   database   is   not   provided   with   the   download,   you   can   easily    
  recreate   it   using   the   design   diagram   above.     Below   is   also   a   snapshot   from   server   explorer   of   the   data:    
     
  Figure   3   -   Snapshot   from   the   Server   Explorer   of   the   Data   in   this   sample    
       
  Alot   of   the   coding   for   this   example   is   done   visually.     I   created   the   sql   server   adapter   and   sql   connection   simply   by   dragging   the    
  table   from   the   server   explorer   into   the   Design   View   of   the   Form.     At   this   point,   you   have   almost   everything   you   need   to   read   the    
  flashcard   information   into   the   Form.     The   code   for   reading   in   the   table   in   Figure   3     is   shown   in   the   listing   below:    
  public   bool   ReadFlashCard(int   x)  
    {  
        try  
        {    
  //   dispose   of   the   old   image   and     set   the   picture   box   to   null  
            if   (pictureBox1.Image   !=   null)  
                    pictureBox1.Image.Dispose();  
                    pictureBox1.Image   =   null;  
  //   form   a   query   that   will   only   select   the   flashcard   we   are   interested   in  
                      sqlDataAdapter1.SelectCommand.CommandText   =   "Select   *   From   Flashcards   Where   PrimaryKey   =   "  
                            +                         Convert.ToString(x);  
  //     Fill   the   dataset   using   the   query   from   the   adapter  
                    DataSet   ds   =   new   DataSet();  
                  sqlDataAdapter1.Fill(ds,   "Flashcards");  
  //     Hide   the   labels   that   contain   the   answer   to   the   flashcard  
                  label1.Visible   =   false;  
                  label2.Visible   =   false;  
  //   if   no   row   is   returned   it   means   we   reached   the   end   or   something   is   wrong          
            if   (ds.Tables[0].Rows.Count   ==   0)  
                      return   false;  
  //   populate   the   hidden   answers:     the   language   word   and   the   pronunciation  
      label1.Text   =   ds.Tables[0].Rows[0]["LearnLanguage"].ToString();  
      label2.Text   =   ds.Tables[0].Rows[0]["Pronunciation"].ToString();  
  //     now   we   need   to   get   the   picture   from   the   DataRow  
  //     and   assign   it   to   a   byte   array  
   
      byte[]   MyData   =   null;  
      MyData   =   (byte[])ds.Tables[0].Rows[0]["Picture"];  
  //     After   retrieving   the   byte   array,   we   want   to   get   the    
  //     number   of   elements   of   the   array   for   use   in   writing   the   array  
        int   ArraySize   =   new   int();  
      ArraySize   =   MyData.GetUpperBound(0);  
  //     Create   a   Filestream   for   writing   the   byte   array   to   a   gif   file   (the   original   format   in   this   case)  
      FileStream   fs   =   new   FileStream("tmp.gif",   FileMode.OpenOrCreate,   FileAccess.Write);  
  //     Write   the   stream   of   data   that   we   read   in   from   the   database   to   the   filestream   and   close   it.  
  //     This   will   create   the   file     tmp.gif,   which   we   can   use   to   display   in   the   picturebox  
      fs.Write(MyData,   0,ArraySize+1);   //   don't   ask   me   why,   but   I   had   to   add   1   here   for   this   to   work  
      fs.Close();  
  //     Assign   the   temporary   gif   file   to   the   picture   box  
      pictureBox1.Image   =   new   Bitmap("tmp.gif");  
      return   true;  
  }  
  catch   (Exception   ex)  
    {  
      MessageBox.Show(ex.Message.ToString());  
      return   false;  
      }  
  }  
  Reading   in   the   byte   data   is   as   simple   as   assigning   the   DataRow   Picture   Column   to   a   byte   array.     Once   we   have   the   byte   array,   we   can    
  use   it   to   create   an   image   file   (in   this   program   we   use   gif   format,   although   you   are   free   to   change   it   to   use   your   own).     The    
  FileStream   class   serves   our   purpose   well   here,   because   it   can   be   used   to   create   a   file   from   byte   data.     Once   we've   created   the    
  temporary   gif   file   on   our   disk,   we   can   assign   it   to   the   picturebox   component.    
  Writing   Image   Data   into   the   Database   is   almost   as   easy.     In   this   application,   we've   created   a   second   form   used   to   populate   the    
  database.     This   form   serves   as   almost   a   modeless   dialog   to   allow   us   to   put   our   flashcards   easily   into   the   Sql   Server   Database:    
     
  Figure   4-   Second   Form   used   to   Populate   the   Database    
  The   Code   for   writing   to   the   Database   is   shown   below:    
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
    try  
      {    
  //     Fill   a   DataSet   with   existing   Flashcards  
          DataSet   ds   =   new   DataSet();  
        sqlDataAdapter1.Fill(ds,   "Flashcards");  
        DataTable   TheTable   =   ds.Tables[0];  
  //   Create   a   new   row   for   the   FlashCard   Table   in   Memory    
        DataRow   aRow   =   TheTable.NewRow();  
       
  //     Insert   the   information   from   the   dialog   into   the   Flashcard   Table    
  //     Such   as   the   Primary   Key,   Language   Word,   Translation,   and   Pronunciation  
        aRow["PrimaryKey"]   =   TheTable.Rows.Count   +   1;  
        aRow["LearnLanguage"]   =   textBox1.Text;  
        aRow["FirstLanguage"]   =   textBox2.Text;  
        aRow["Pronunciation"]   =   textBox3.Text;  
  //     Create   a   new   FileStream   for   reading   data   from   the   image   file     in   which   the   PictureBox   populated  
  //     Its   data   from   (the   FileName   is   maintained   after   the   picture   button   is   pressed   and   a   picture   is   selected).  
   
        FileStream   fs   =   new   FileStream   (FileName,   FileMode.OpenOrCreate,  
                                                                                                  FileAccess.Read);  
  //       Read   the   Data   into   the   Byte   Array  
          byte[]   MyData   =   new   byte[fs.Length];  
        fs.Read(MyData,   0,   (int)fs.Length);  
        fs.Close();  
  //     Assign   the   DataRow   Picture   Column   to   the   Byte   Array   to   populate   it   in   the   DataRow  
   
        aRow["Picture"]   =   MyData;  
  //       Add   the   DataRow   to   the   DataTable  
   
        TheTable.Rows.Add(aRow);  
  //     Write   all     the   data   (including   the   picture)   to   the   Flashcards   Sql   Server   database  
   
        sqlDataAdapter1.Update(ds,   "Flashcards");  
          MessageBox.Show("Flashcard   Added.");  
  }  
  catch   (Exception   ex)  
  {  
          MessageBox.Show(ex.Message.ToString());  
  }  
  }  
  The   code   above   for   writing   a   picture   to   the   database   is   very   similar   to   reading,   only   performed   in   the   opposite   direction.     First    
  you   read   the   data   into   a   byte   array   using   the   FileStream   class.     Then   you   assign   the   DataRow   Picture   Item   to   the   byte   array   data    
  that   you   just   read.     Finally,   call   Update   on   the   Adapter   to   force   it   to   write   the   picture   data   (along   with   the   textual   data)   out   to    
  the   database.  
  Improvements  
  One   thing   that   would   be   nice   to   do   in   this   program   is   to   allow   for   font   changes   and   store   the   font   information   in   the   database.      
  This   way   you   could   adapt   this   program   to   work   with   any   language   and   any   language   character   set.  
   
   
   
   
   
  Top

5 楼91bct(Jerry)回复于 2004-09-01 10:43:47 得分 0

用datagrid來顯示數據庫中的圖片的代碼:  
   
  retrieveimgbydatagrid.aspx  
  -----  
  <%@   Page   Language="vb"   Debug="true"   %>  
  <%@   Import   Namespace="System.Data"   %>  
  <%@   Import   Namespace="System.Data.OleDb"   %>  
  <script   language="VB"   runat="server">  
    dim   MyConnection   As   OleDbConnection  
   
    Sub   Page_Load(s   As   Object,   E   As   EventArgs)  
            MyConnection   =   New   OleDbConnection(   "Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source   ="   +   Server.MapPath(".")+"/db/kk.mdb")  
                  If   Not   (IsPostBack)   then  
                  BindDataGrid()  
                  End   If  
    End   Sub  
   
  Sub   BindDataGrid()  
          dim   dadTitles   as   OleDbDataAdapter  
          dim   dstTitles   as   DataSet  
          dadTitles=new   OleDbDataAdapter("select   *   from   [imgs]   order   by   id   desc",MyConnection)  
  dstTitles=new   DataSet  
  dadTitles.fill(dstTitles)  
  MyDataGrid.DataSource=dstTitles  
  MyDataGrid.DataBind()  
  End   Sub  
   
  Sub   Mydatagrid_PageIndexChanged(s   as   object,e   as   DataGridPageChangedEventargs)  
          MyDataGrid.CurrentPageIndex=e.newPageIndex  
  BindDataGrid()  
  End   Sub  
   
  Function   FormatURL(strArgument)   as   String  
              Return   ("RetrieveImgByDataGrid_SelectImg.aspx?id="   &   strArgument)  
  End   Function  
   
  </script>  
  <html>  
  <head><title>DataGrid_CheckBox.aspx</title></head>  
  <body>  
  <B><A   HREF="insertimgtodatabase.aspx">Insert   Img   To   DataBase</A></B>  
  <form   runat="server">  
  <asp:DataGrid   id="MyDataGrid"  
                              width="300"  
                              Runat="server"  
                              AllowPaging="true"    
      pageSize="5"    
      OnPageIndexChanged="Mydatagrid_PageIndexChanged"    
      cellPadding="3"  
      AutoGenerateColumns="false"    
      HeaderStyle-BackColor="#ff0000"  
              HeaderStyle-Font-Bold="True"  
              HeaderStyle-Font-Name="Verdana"  
              HeaderStyle-Font-Size="13px"  
              HeaderStyle-ForeColor="#ffffff"  
              ItemStyle-BackColor=Beige  
              ItemStyle-Font-Name="verdana"  
              ItemStyle-Font-Size="13px"  
      >  
                              <PagerStyle   Mode="NumericPages"  
                              Font-Bold="true"    
                              BackColor="#FFCC99"  
                              HorizontalAlign="right"    
      >          
      </PagerStyle>  
            <Columns>  
  <asp:TemplateColumn   HeaderText="ID">  
  <ItemTemplate>  
                  <asp:Label   ID="lblID"     Text='<%#   DataBinder.Eval(Container.DataItem,   "id")   %>'   Runat=server   />  
   
  </ItemTemplate>    
  </asp:TemplateColumn>    
  <asp:TemplateColumn   HeaderText="NAME">  
  <ItemTemplate>  
                  <asp:Label   ID="lblName"     Text='<%#   DataBinder.Eval(Container.DataItem,   "name")   %>'   Runat=server   />  
   
  </ItemTemplate>    
  </asp:TemplateColumn>    
  <asp:TemplateColumn   HeaderText="Image">  
                          <ItemTemplate>  
                              <asp:Image     Width="150"   Height="125"   ImageUrl='<%#   FormatURL(DataBinder.Eval(Container.DataItem,   "ID"))   %>'    
  Runat=server   />  
                          </ItemTemplate>                                                                
                </asp:TemplateColumn>  
        <asp:TemplateColumn   HeaderText="ShowImage_HyperLink">  
                          <ItemTemplate>  
                              <asp:HyperLink     Width="150"   Height="125"   ImageUrl='<%#   FormatURL(DataBinder.Eval(Container.DataItem,   "ID"))   %>'    
  NavigateUrl='<%#   "RetrieveImgByDataGrid_SelectImg.aspx?id="   &   DataBinder.Eval(Container.DataItem,   "ID")%>'   Runat="server"   />  
                          </ItemTemplate>                                                                
                </asp:TemplateColumn>                                        
      </Columns>  
  </asp:DataGrid>  
  </form>  
  </body>  
  </html>  
   
   
   
   
   
   
   
   
   
   
   
  RetrieveImgByDataGrid_SelectImg.aspx  
  ----  
  <%@   Page   Language="vb"   %>  
  <%@   Import   Namespace="System.Data"   %>  
  <%@   Import   Namespace="System.Data.OleDb"   %>  
  <HTML>  
      <HEAD>  
          <title>Retrieving   Image   from   the   Sql   Server</title>  
          <script   runat=server>  
        dim   MyConnection   as   OleDbConnection  
                dim   ID   as   Integer  
   
                Public   Sub   Page_Load(sender   As   Object,   e   As   EventArgs)  
                  ID=Request.QueryString("id")  
   
                  'Create   Instance   of   Connection   and   Command   Object  
                  MyConnection   =   New   OleDbConnection(   "Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source   ="   +   Server.MapPath(".")+"/db/kk.mdb")  
   
                  Dim   myCommand   As   New   OleDbCommand("SELECT   *   FROM   [imgs]   WHERE   ID="   &ID,   myConnection)  
                      Try  
                              myConnection.Open()  
                              Dim   myDataReader   as   OleDbDataReader    
                              myDataReader   =   myCommand.ExecuteReader(CommandBehavior.CloseConnection)  
                            Do   While   (myDataReader.Read())  
                                    'Response.Write   (myDataReader.Item("id"))  
                                    Response.BinaryWrite   (myDataReader.Item("Img"))  
                            Loop  
                              myConnection.Close()  
                              Response.Write("Person   info   successfully   retrieved!")  
                          Catch   SQLexc   As   OleDbException  
                              Response.Write("Read   Failed   :   "   &   SQLexc.ToString())  
                          End   Try  
                    End   Sub          
      </script>  
      </HEAD>  
      <body   style="font:   10pt   verdana">  
      <asp:Label   id="ShowName"   runat="server"/>  
      </body>  
  </HTML>  
  Top

6 楼91bct(Jerry)回复于 2004-09-01 10:47:24 得分 0

图片存取(sql2000)  
  可以在csdn上搜索到现成的代码,应该说是比较容易的。  
  上面这些只是读取的相关资料。Top

7 楼tianjh(關東闔)回复于 2004-11-02 15:14:27 得分 0

goodTop

相关问题

  • OFFIC文档在SQLSERVER中的存储、读取、修改
  • 在vc中通过ado如何读取sqlserver的存储过程的返回值?
  • 图片存储和读取的问题
  • 大文本的存储和读取?
  • 怎样读取、存储autocad文件
  • 关于用DataSet读取存储过程
  • 链表的存储与读取问题
  • 有关读取EXCEL的操作
  • vb文件读取操作,sos!
  • 文件读取操作,求教

关键词

得分解答快速导航

  • 帖主:12345521

相关链接

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

广告也精彩

反馈

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