亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Storing and Retrieving Images from SQL Serve

系統 2300 0

原文? Storing and Retrieving Images from SQL Server using Microsoft .NET

Introduction

This article is about storing and retrieving images from database in Microsoft .NET using C#.

Tools Used

  • SQL Server 2000
  • Microsoft .NET Version 1.1
  • C# (Windows Forms based application)

Storing Images

  1. Create a table in a SQL Server 2000 database which has at least one field of type? IMAGE .

    Here is the script I used:

    ?Collapse ?|? Copy Code
              
                CREATE
              
              
                TABLE
              
               [dbo].[tblImgData] (
    
    
    
            [ID] [
              
                int
              
              ] 
              
                NOT
              
              
                NULL
              
               ,
    
    
    
            [Name] [
              
                varchar
              
              ] (
              
                50
              
              ) 
              
                COLLATE
              
               SQL_Latin1_General_CP1_CI_AS 
              
                NULL
              
               ,
    
    
    
            [Picture] [
              
                image
              
              ] 
              
                NULL
              
               
    
    
    
    ) 
              
                ON
              
               [
              
                PRIMARY
              
              ] TEXTIMAGE_ON [
              
                PRIMARY
              
              ]
            
  2. Actually? IMAGE ?field is just holding the reference to the page containing the binary data so we have to convert our image into bytes.
    1. I used a file open dialog box to locate the file.
      ?Collapse ?|? Copy Code
                    
                      this
                    
                    .openFileDialog1.ShowDialog(
                    
                      this
                    
                    );
      
      
                    
                      string
                    
                     strFn=this.openFileDialog1.FileName;
                  
    2. By using? FileInfo ?class, I retrieved the file size:
      ?Collapse ?|? Copy Code
                    FileInfo fiImage=new FileInfo(strFn);
                  
    3. Declare an array of that size.
      ?Collapse ?|? Copy Code
                    
                      this
                    
                    .m_lImageFileLength=fiImage.Length;
      
      m_barrImg=new 
                    
                      byte
                    
                    [Convert.ToInt32(
                    
                      this
                    
                    .m_lImageFileLength)];
                  
    4. By using? FileStream ?object, I filled the byte array.
      ?Collapse ?|? Copy Code
                    FileStream fs=new FileStream(strFn,FileMode.Open, 
      
                        FileAccess.Read,FileShare.Read);
      
      
                    
                      int
                    
                     iBytesRead=fs.Read(m_barrImg,
                    
                      0
                    
                    ,
      
                     Convert.ToInt32(
                    
                      this
                    
                    .m_lImageFileLength));
      
      fs.Close();
                  

    Complete Load Image Code

    ?Collapse ?|? Copy Code
              
                protected
              
              
                void
              
               LoadImage()
    
    {
    
        
              
                try
              
              
    
        {
    
            
              
                this
              
              .openFileDialog1.ShowDialog(
              
                this
              
              );
    
            
              
                string
              
               strFn=this.openFileDialog1.FileName;
    
            
              
                this
              
              .pictureBox1.Image=Image.FromFile(strFn);
    
            FileInfo fiImage=new FileInfo(strFn);
    
            
              
                this
              
              .m_lImageFileLength=fiImage.Length;
    
            FileStream fs=new FileStream(strFn,FileMode.Open, 
    
                              FileAccess.Read,FileShare.Read);
    
            m_barrImg=new 
              
                byte
              
              [Convert.ToInt32(
              
                this
              
              .m_lImageFileLength)];
    
            
              
                int
              
               iBytesRead = fs.Read(m_barrImg,
              
                0
              
              , 
    
                             Convert.ToInt32(
              
                this
              
              .m_lImageFileLength));
    
            fs.Close();
    
        }
    
        
              
                catch
              
              (Exception ex)
    
        {
    
            MessageBox.Show(ex.Message);
    
        }
    
    }
            
  3. Saving byte array data to database.
    1. Create command text to insert record.
      ?Collapse ?|? Copy Code
                    
                      this
                    
                    .sqlCommand1.CommandText= 
      
        
                    
                      "
                    
                    
                      INSERT INTO tblImgData(ID,Name,Picture)"
                    
                     + 
      
        
                    
                      "
                    
                    
                       values(@ID,@Name,@Picture)"
                    
                    ;
                  
    2. Create parameters.
      ?Collapse ?|? Copy Code
                    
                      this
                    
                    .sqlCommand1.Parameters.Add(
                    
                      "
                    
                    
                      @ID"
                    
                    ,
      
                 System.Data.SqlDbType.Int, 
                    
                      4
                    
                    );
      
      
                    
                      this
                    
                    .sqlCommand1.Parameters.Add(
                    
                      "
                    
                    
                      @Name"
                    
                    , 
      
                 System.Data.SqlDbType.VarChar, 
                    
                      50
                    
                    );
      
      
      
      
                    
                      this
                    
                    .sqlCommand1.Parameters.Add(
                    
                      "
                    
                    
                      @Picture"
                    
                    , 
      
                 System.Data.SqlDbType.Image);
                  

      Notice “ @Picture ” has “ SqlDbType.Image ” because it is of? IMAGE ?type Field.

    3. Provide the value to the parameters.
      ?Collapse ?|? Copy Code
                    
                      this
                    
                    .sqlCommand1.Parameters[
                    
                      "
                    
                    
                      @ID"
                    
                    ].Value=this.editID.Text;
      
      
                    
                      this
                    
                    .sqlCommand1.Parameters[
                    
                      "
                    
                    
                      @Name"
                    
                    ].Value=this.editName.Text;
      
      
      
      
                    
                      this
                    
                    .sqlCommand1.Parameters[
                    
                      "
                    
                    
                      @Picture"
                    
                    ].Value=this.m_barrImg;
                  

      this .m_barrImg ” is a byte array which we filled in the previous step.

    4. Now execute non-query for saving the record to the database.
      ?Collapse ?|? Copy Code
                    
                      int
                    
                     iresult=this.sqlCommand1.ExecuteNonQuery();
                  

    Complete Save Image Code

    ?Collapse ?|? Copy Code
              
                private
              
              
                void
              
               btnSave_Click(
              
                object
              
               sender, System.EventArgs e)
    
    {
    
        
              
                try
              
              
    
        {
    
            
              
                this
              
              .sqlConnection1.Open();
    
            
              
                if
              
               (sqlCommand1.Parameters.Count ==0 )
    
            {
    
                
              
                this
              
              .sqlCommand1.CommandText=
              
                "
              
              
                INSERT INTO tblImgData(ID,"
              
               + 
    
                               
              
                "
              
              
                 Name,Picture) values(@ID,@Name,@Picture)"
              
              ;
    
                
              
                this
              
              .sqlCommand1.Parameters.Add(
              
                "
              
              
                @ID"
              
              , 
    
                                 System.Data.SqlDbType.Int,
              
                4
              
              );
    
                
              
                this
              
              .sqlCommand1.Parameters.Add(
              
                "
              
              
                @Name"
              
              , 
    
                                 System.Data.SqlDbType.VarChar,
              
                50
              
              );
    
                
              
                this
              
              .sqlCommand1.Parameters.Add(
              
                "
              
              
                @Picture"
              
              , 
    
                                 System.Data.SqlDbType.Image);
    
            }
    
    
    
            
              
                this
              
              .sqlCommand1.Parameters[
              
                "
              
              
                @ID"
              
              ].Value=this.editID.Text;
    
            
              
                this
              
              .sqlCommand1.Parameters[
              
                "
              
              
                @Name"
              
              ].Value=this.editName.Text;
    
            
              
                this
              
              .sqlCommand1.Parameters[
              
                "
              
              
                @Picture"
              
              ].Value=this.m_barrImg;
    
    
    
            
              
                int
              
               iresult=this.sqlCommand1.ExecuteNonQuery();
    
            MessageBox.Show(Convert.ToString(iresult));
    
        }
    
        
              
                catch
              
              (Exception ex)
    
        {
    
            MessageBox.Show(ex.Message);
    
        }
    
        
              
                finally
              
              
    
        {
    
            
              
                this
              
              .sqlConnection1.Close();
    
        }
    
    }
            

Retrieving Image

Retrieving images from the database is the exact reverse process of saving images to the database.

  1. First create command text to retrieve record.
    ?Collapse ?|? Copy Code
              SqlCommand cmdSelect = 
              
                new
              
               SqlCommand(
              
                "
              
              
                select Picture"
              
               + 
    
                           
              
                "
              
              
                 from tblImgData where ID=@ID"
              
              , 
    
                           
              
                this
              
              .sqlConnection1);
            
  2. Create parameter for the query.
    ?Collapse ?|? Copy Code
              cmdSelect.Parameters.Add(
              
                "
              
              
                @ID"
              
              ,SqlDbType.Int,
              
                4
              
              );
            
  3. Provide value to the parameter.
    ?Collapse ?|? Copy Code
              cmdSelect.Parameters[
              
                "
              
              
                @ID"
              
              ].Value=this.editID.Text;
            
  4. Open database connection and execute “ ExecuteScalar ” because we want only “ IMAGE ” column data back.
    ?Collapse ?|? Copy Code
              
                byte
              
              [] barrImg=(
              
                byte
              
              [])cmdSelect.ExecuteScalar();
            

    As the execute scalar returns data of “ Object ” data type, we cast it to? byte ?array.

  5. Save this data to a temporary file.
    ?Collapse ?|? Copy Code
              
                string
              
               strfn=Convert.ToString(DateTime.Now.ToFileTime());
    
    FileStream fs=new FileStream(strfn,FileMode.CreateNew,FileAccess.Write);
    
    fs.Write(barrImg,
              
                0
              
              ,barrImg.Length);
    
    fs.Flush();
    
    fs.Close();
            
  6. And display the image anywhere you want to display.
    ?Collapse ?|? Copy Code
              pictureBox1.Image=Image.FromFile(strfn);
            

Complete Image Retrieving Code

?Collapse ?|? Copy Code
      
        private
      
      
        void
      
       btnLoad_Click(
      
        object
      
       sender, System.EventArgs e)

{

    
      
        try
      
      

    {

        SqlCommand cmdSelect=new SqlCommand(
      
        "
      
      
        select Picture"
      
       + 

              
      
        "
      
      
         from tblImgData where ID=@ID"
      
      ,
      
        this
      
      .sqlConnection1);

        cmdSelect.Parameters.Add(
      
        "
      
      
        @ID"
      
      ,SqlDbType.Int,
      
        4
      
      );

        cmdSelect.Parameters[
      
        "
      
      
        @ID"
      
      ].Value=this.editID.Text;



        
      
        this
      
      .sqlConnection1.Open();

        
      
        byte
      
      [] barrImg=(
      
        byte
      
      [])cmdSelect.ExecuteScalar();

        
      
        string
      
       strfn=Convert.ToString(DateTime.Now.ToFileTime());

        FileStream fs=new FileStream(strfn, 

                          FileMode.CreateNew, FileAccess.Write);

        fs.Write(barrImg,
      
        0
      
      ,barrImg.Length);

        fs.Flush();

        fs.Close();

        pictureBox1.Image=Image.FromFile(strfn);

    }

    
      
        catch
      
      (Exception ex)

    {

        MessageBox.Show(ex.Message);

    }

    
      
        finally
      
      

    {

        
      
        this
      
      .sqlConnection1.Close();

    }

}
    

Bibliography

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found? here

Storing and Retrieving Images from SQL Server using Microsoft .NET


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 四虎国产精品永久在线播放 | 久久亚洲精品专区蓝色区 | 亚洲精品欧美一区二区三区 | 91久久精品国产91性色tv | 看全色黄大色大片免费久黄久 | 国产成人精品日本亚洲语音2 | 久久只有精品 | 亚州免费一级毛片 | 91手机在线观看 | 久久99精品视香蕉蕉 | 国语自产偷拍精品视频偷最新 | 国产极品嫩模在线观看91精品 | 中文字幕亚洲综合 | 日本久久中文字幕 | 国产图片亚洲精品一区 | 日本精品久久久久中文字幕 | 中文字幕国产亚洲 | 国产91小视频在线观看 | 欧美精品aaa久久久影院 | xx性欧美高清 | 乡下女色又黄一级毛片 | 欧美成人观看视频在线 | 永久免费观看黄网站 | 久久精品久久久久 | 欧美日本激情 | 伊人色婷婷综在合线亚洲 | 香蕉视频在线免费 | 99热热久久这里只有精品166 | 美女一级毛片免费观看 | 九九99久久精品国产 | 久久久青青久久国产精品 | 全部精品孕妇色视频在线 | 中国性xxxxx极品奶水 | 久久久国产一区二区三区 | 日日夜夜影院 | 天天槽天天槽天天槽 | 欧美一级久久久久久久久大 | 亚洲一区在线免费观看 | 国产99久久| 免费观看日本高清a毛片 | 精品无人区乱码一区2区3区 |