国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET如何存取SQL Server數據庫圖片

2024-07-10 13:03:00
字體:
來源:轉載
供稿:網友

    sql server提供了一個特別的數據類型:image,它是一個包含binary數據的類型。下邊這個例子就向你展示了如何將文本或照片放入到數據庫中的辦法。在這篇文章中我們要看到如何在sql server中存儲和讀取圖片。 
   
     1、建立一個表:
  
    在sql server中建立這樣結構的一個表:
  
  列名 類型 目的
  id integer 主鍵id
  imgtitle varchar(50) 圖片的標題
  imgtype varchar(50) 圖片類型. asp.net要以辨認的類型
  imgdata image 用于存儲二進制數據
  
    2、存儲圖片到sql server數據庫中
  
    為了能存儲到表中,你首先要上傳它們到你的web 服務器上,你可以開發一個web form,它用來將客戶端中textbox web control中的圖片入到你的web服務器上來。將你的 enctype 屬性設置為:myltipart/formdata. 
   
  stream imgdatastream = file1.postedfile.inputstream;
  int imgdatalen = file1.postedfile.contentlength;
  string imgtype = file1.postedfile.contenttype;
  string imgtitle = textbox1.text;
  byte[] imgdata = new byte[imgdatalen];
  int n = imgdatastream.read(imgdata,0,imgdatalen);
  string connstr=((namevaluecollection)context.getconfig("appsettings"))["connstr"];
  
  sqlconnection connection = new sqlconnection(connstr);
  
  sqlcommand command = new sqlcommand
           ("insert into imagestore(imgtitle,imgtype,imgdata)
           values ( @imgtitle, @imgtype,@imgdata )", connection );
  
  sqlparameter paramtitle = new sqlparameter
           ("@imgtitle", sqldbtype.varchar,50 );
  
  paramtitle.value = imgtitle;
  command.parameters.add( paramtitle);
  
  sqlparameter paramdata = new sqlparameter( "@imgdata", sqldbtype.image );
  paramdata.value = imgdata;
  command.parameters.add( paramdata );
  
  sqlparameter paramtype = new sqlparameter( "@imgtype", sqldbtype.varchar,50 );
  paramtype.value = imgtype;
  command.parameters.add( paramtype );
  
  connection.open();
  int numrowsaffected = command.executenonquery();
  connection.close();
  
    3、從數據庫中恢復讀取
  
    現在讓我們來從sql server中讀取我們放入的數據吧!我們將要輸出圖片到你的瀏覽器上,你也可以將它存放到你要的位置。
  
  private void page_load(object sender, system.eventargs e)
  {
   string imgid =request.querystring["imgid"];
   string connstr=((namevaluecollection)
   context.getconfig("appsettings"))["connstr"];
   string sql="select imgdata, imgtype from imagestore where id = " + imgid;
   sqlconnection connection = new sqlconnection(connstr);
   sqlcommand command = new sqlcommand(sql, connection);
   connection.open();
   sqldatareader dr = command.executereader();
   if(dr.read())
   {
    response.contenttype = dr["imgtype"].tostring();
    response.binarywrite( (byte[]) dr["imgdata"] );
   }
   connection.close();
  }
  
    要注意的是response.binarywrite 而不是response.write.
  
    下面給大家一個用于c# winform的存入、讀取程序。其中不同請大家自己比較!(為了方便起見,我將數據庫字段簡化為二個:imgtitle和imgdata。
  
  using system;
  using system.drawing;
  using system.collections;
  using system.componentmodel;
  using system.windows.forms;
  using system.data;
  using system.io;
  using system.data.sqlclient;
  
  namespace windowsapplication21
  {
   /// <summary>
   /// form1 的摘要說明。
   /// </summary>
   public class form1 : system.windows.forms.form
   {
    private system.windows.forms.button button1;
    /// <summary>
    /// 必需的設計器變量。
    /// </summary>
    private system.componentmodel.container components = null;
    private string connectionstring = "integrated security=sspi;initial catalog=;data source=localhost;";
    private sqlconnection conn = null;
    private sqlcommand cmd = null;
    private system.windows.forms.button button2;
    private system.windows.forms.picturebox pic1;
    private system.windows.forms.openfiledialog openfiledialog1;
    private string sql = null;
    private system.windows.forms.label label2;
    private string nowid=null;
  
   public form1()
   {
    //
    // windows 窗體設計器支持所必需的
    //
    initializecomponent();
    conn = new sqlconnection(connectionstring);
  
    //
    // todo: 在 initializecomponent 調用后添加任何構造函數代碼
    //
   }
  
   /// <summary>
   /// 清理所有正在使用的資源。
   /// </summary>
   protected override void dispose( bool disposing )
   {
    if (conn.state == connectionstate.open)
     conn.close();
    if( disposing )
    {
     if (components != null)
     {
      components.dispose();
     }
    }
    base.dispose( disposing );
  
   }
 

,歡迎訪問網頁設計愛好者web開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富蕴县| 宜良县| 泰州市| 道孚县| 大宁县| 呼和浩特市| 东乌珠穆沁旗| 新河县| 连平县| 陈巴尔虎旗| 嘉禾县| 肇东市| 济南市| 舒城县| 佛教| 图片| 吉水县| 绥中县| 红桥区| 娄烦县| 定安县| 永吉县| 财经| 绍兴县| 吉木乃县| 禄丰县| 呼和浩特市| 资中县| 尼木县| 阿拉尔市| 讷河市| 兴宁市| 留坝县| 福泉市| 揭西县| 镇坪县| 绵竹市| 大化| 泗洪县| 新昌县| 朝阳市|