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

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

如何在SQL Server中保存和輸出圖片

2024-08-31 00:48:57
字體:
供稿:網(wǎng)友
國內(nèi)最大的酷站演示中心!
建表   

為了試驗這個例子你需要一個含有數(shù)據(jù)的table(你可以在現(xiàn)在的庫中創(chuàng)建它,也可以創(chuàng)建一個新的數(shù)據(jù)庫),下面是它的結(jié)構(gòu):  

   column name    datatype    purpose    id    integer    identity column primary key    imgtitle    varchar(50)    stores some user friendly title to identity the image    imgtype    varchar(50)    stores image content type. this will be same as recognized content types of asp.net    imgdata    image    stores actual image or binary data.

保存images進(jìn)sql server數(shù)據(jù)庫  

為了保存圖片到table你首先得從客戶端上傳它們到你的web服務(wù)器。你可以創(chuàng)建一個web form,用textbox得到圖片的標(biāo)題,用html file server control得到圖片文件。確信你設(shè)定了form的enctype屬性為multipart/form-data。  

   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();   

從數(shù)據(jù)庫中輸出圖片   

現(xiàn)在讓我們從數(shù)據(jù)庫中取出我們剛剛保存的圖片,在這兒,我們將直接將圖片輸出至瀏覽器。你也可以將它保存為一個文件或做任何你想做的。

   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();    }   

在上面的代碼中我們使用了一個已經(jīng)打開的數(shù)據(jù)庫,通過datareader選擇images。接著用response.binarywrite代替response.write來顯示image文件。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 农安县| 玉龙| 永和县| 南投市| 樟树市| 通化市| 白城市| 天台县| 铜川市| 桂东县| 怀仁县| 察哈| 莲花县| 建水县| 嫩江县| 冷水江市| 灵寿县| 龙海市| 漠河县| 赫章县| 东乡| 鹿邑县| 建始县| 东方市| 叙永县| 嘉禾县| 乌鲁木齐县| 布尔津县| 新民市| 玉溪市| 洪江市| 无锡市| 宁阳县| 平果县| 洛隆县| 文山县| 莲花县| 广宗县| 靖西县| 海林市| 三穗县|