|
本人試圖使用如下代碼取出sql數據庫中的image類型的數據, 但是提示“指定的轉換無效”。 1、請問如何取出sql數據庫中的image類型的數據? 2、請問sql數據庫中的image類型存放的是不是圖片在外存的絕對地址? image類型數據的真正含義是什么? 感謝您的回復??! -- //打開連接 con.open(); string sql="select id,img from test where id="+convert.toint32(this.textbox1.text)+""; da2=new sqldataadapter(sql,con); //用相關的數據填充數據容器dataset da2.fill(ds,"ds2"); this.picturebox2.image = (system.drawing.image)ds.tables["ds2"].rows[0][1]; con.close();
參考文章:
apache+php 的匿名FTP功能目錄權限設置問題
Linux下如何限制訪問apache的每個用戶的瀏覽進程?
想讓php支持snmp,gd等模塊(內詳),請問這個怎么安裝 ?
Apache進行Rewrite時需要注意根目錄
apache中與日志有關的模塊
redhat9下使用apache2.0實現個人主頁空間的方法1
實戰(zhàn)apache用戶驗證
mod_vhost_alias問題
apache+mod_ssl初學者指南
緊急問題!apache2.0下如何限制虛擬主機同時并發(fā)連接數
發(fā)表者:twodays
how to:在 visual c# 中直接將一個圖片從數據庫復制到 picturebox 控件 適用于 有關本文的 microsoft visual basic .net 版本,請參閱 317670。 本任務的內容 摘要 要求 示例 缺陷 參考 概要 本分步指南介紹如何將存儲在數據庫中的圖像直接復制到 windows 窗體上的 picturebox 控件,而無須將此圖像保存到文件。
在 microsoft visual basic 6.0 中,如想直接在 picturebox
控件中顯示數據庫中的圖像而不經過將二進制大對象 {blob) 數據保存到文件這一中間步驟,唯一的方法是將 picturebox
綁定到一個數據源,如 activex data objects (ado) 數據控件或記錄集。若不將圖像保存到文件以供
loadpicture 語句使用,則沒有辦法以編程方式將 blob 加載到控件。 在本文中,我們將使用 system.io 基類中的 memorystream 對象將圖像數據直接從數據庫復制到 picturebox 控件。 返回頁首 要求 下表概括了推薦使用的硬件、軟件、網絡結構以及所需的 service pack: 安裝在兼容 microsoft windows 操作系統(tǒng)中的 microsoft visual studio .net 用于測試的可用 microsoft sql server 實例或可用 microsoft access 數據庫 本文假定您熟悉下列主題: visual c# .net windows 窗體應用程序 數據庫中的二進制大對象 (blob) 存儲 ado.net 數據訪問 返回頁首 示例 使用以下結構創(chuàng)建一個 sql server 或 access 表:create table blobtest ( blobid int identity not null, blobdata image not null ) 打開 visual studio .net,然后新建一個 visual c# windows 應用程序項目。
從工具箱向默認的 form1 添加一個 picturebox 和兩個 button 控件。將 button1
的 text 屬性設置為 file to database,并將 button2 的 text
屬性設置為 database to picturebox。 在窗體的代碼模塊頂部插入 using 語句:using system.data.sqlclient; using system.io; using system.drawing.imaging;
將以下數據庫連接字符串的聲明添加到 public class form1
:system.windows.forms.form 類聲明中,并根據需要調整連接字符串: string strcn
= "data source=localhost;integrated security=sspi;initial
catalog=mydata"; 將下面的代碼插入 button1 (file
to database) 的 click
事件過程中。根據需要調整到一個可用示例圖像文件的可用路徑。此代碼可將圖像文件從磁盤讀入 byte 數組,然后使用一個參數化的
command 對象將數據插入數據庫。try { sqlconnection cn = new sqlconnection(strcn); sqlcommand cmd = new sqlcommand("insert into blobtest (blobdata) values (@blobdata)", cn); string strblobfilepath = @"c:\blue hills.jpg";//modify this path as needed. //read jpg into file stream, and from there into byte array. filestream fsblobfile = new filestream(strblobfilepath,filemode.open, fileaccess.read); byte[] bytblobdata = new byte[fsblobfile.length]; fsblobfile.read(bytblobdata, 0, bytblobdata.length); fsblobfile.close(); //create parameter for insert command and add to sqlcommand object.
sqlparameter prm = new sqlparameter("@blobdata",
sqldbtype.varbinary, bytblobdata.length, parameterdirection.input,
false, 0, 0, null, datarowversion.current, bytblobdata); cmd.parameters.add(prm); //open connection, execute query, and close connection. cn.open(); cmd.executenonquery(); cn.close(); }catch(exception ex) {messagebox.show(ex.message);}
將下面的代碼插入 button2 (database to picturebox) 的 click
事件過程。此代碼將行從數據庫中的 blobtest 表檢索到一個數據集,復制最新添加的圖像到 byte 數組,然后到
memorystream 對象,接著將 memorystream 加載到 picturebox 控件的 image
屬性。try { sqlconnection cn = new sqlconnection(strcn); cn.open(); //retrieve blob from database into dataset. sqlcommand cmd = new sqlcommand("select blobid, blobdata from blobtest order by blobid", cn); sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds, "blobtest"); int c = ds.tables["blobtest"].rows.count; if(c>0) { //blob is read into byte array, then used to construct memorystream, //then passed to picturebox. byte[] byteblobdata = new byte[0]; byteblobdata = (byte[])(ds.tables["blobtest"].rows[c - 1]["blobdata"]); memorystream stmblobdata = new memorystream(byteblobdata); picturebox1.image= image.fromstream(stmblobdata); } cn.close(); } catch(exception ex) {messagebox.show(ex.message);} 按 f5 鍵編譯并運行該項目。 單擊 file to database 按鈕將至少一個示例圖像加載到數據庫。 單擊 database to picturebox 按鈕將保存的圖像顯示在 picturebox 控件中。
如果想能夠直接將圖像從 picturebox 控件插入數據庫,則請?zhí)砑拥谌齻€ button 控件,并將下面的代碼插入其
click 事件過程。此代碼將圖像數據從 picturebox 控件檢索到 memorystream 對象,將
memorystream 復制到一個 byte 數組,然后使用一個參數化的 command 對象將 byte
數組保存到數據庫。try { sqlconnection cn = new sqlconnection(strcn); sqlcommand cmd = new sqlcommand("insert into blobtest (blobdata) values (@blobdata)", cn); //save image from picturebox into memorystream object. memorystream ms = new memorystream(); picturebox1.image.save(ms, imageformat.jpeg); //read from memorystream into byte array. byte[] bytblobdata = new byte[ms.length]; ms.position = 0; ms.read(bytblobdata, 0, convert.toint32(ms.length)); //create parameter for insert statement that contains image.
sqlparameter prm = new sqlparameter("@blobdata",
sqldbtype.varbinary, bytblobdata.length, parameterdirection.input,
false, 0, 0,null, datarowversion.current, bytblobdata); cmd.parameters.add(prm); cn.open(); cmd.executenonquery(); cn.close(); }catch(exception ex) {messagebox.show(ex.message);}
運行該項目。單擊 database to picturebox 按鈕以顯示剛才在 picturebox
控件中保存過的圖像。單擊新添加的按鈕將此圖像從 picturebox 保存到數據庫。然后再次單擊 database to
picturebox 按鈕以確認圖像已正確保存。 返回頁首 缺陷
此測試不適用于 access 和 sql server
中的羅斯文示例數據庫的雇員表中的照片列。存儲在照片列中的位圖圖像用由 visual basic 6.0 ole
container 控件創(chuàng)建的標題信息進行了包裝。 如果需要使用 access 數據庫測試此代碼,則需要在
access 表中創(chuàng)建一個 ole object 類型的列,并使用 microsoft jet 4.0
provider 中的 system.data.oledb 名稱空間代替 system.data.sqlclient
名稱空間。
發(fā)表者:twodays
how to:在 visual c# .net 中通過使用 ado.net 讀寫 blob 數據 適用于 本文的發(fā)布號曾為 chs309158 有關本文的 microsoft visual basic .net 版本,請參見 308042。 有關本文的 microsoft visual j# .net 版本,請參見 320629。 本文引用下面的 microsoft .net 框架類庫名稱空間: system.data.sqlclient system.io 本任務的內容 概要 要求 創(chuàng)建項目 概要
在 ado.net 中,datareader 列、dataset 列或 command 參數不能使用
getchunk 和 appendchunk 方法。本文介紹如何使用 visual c# .net
讀寫二進制大對象 (blob) 字段。 返回頁首 要求 下面的列表列出了推薦使用的硬件、軟件、網絡結構以及所需的 service pack:
microsoft windows 2000 professional、windows 2000
server、windows 2000 advanced server 或 windows nt 4.0
server microsoft visual studio .net microsoft sql server 返回頁首 創(chuàng)建項目 在您的 sql server 羅斯文數據庫中添加一個名為 myimages 的表。在該表中包含以下字段: 標識字段,名為"id",類型為 int。 字段,名為"description",類型為 varchar,長度為 50。 字段,名為"imgfield",類型為 image。 啟動 visual studio .net,然后新建一個 visual c# windows 應用程序項目。 將兩個 button 控件從工具箱拖到默認窗體 form1 上。 在"屬性"窗口中,將 button1 的 text 屬性更改為保存到數據庫,將 button2 的 text 屬性更改為保存到文件。 將下面的代碼添加到"代碼"窗口頂部: using system.data; using system.data.sqlclient; using system.io; 雙擊 button1,然后將以下代碼添加到 button1_click 事件處理程序中: { sqlconnection con = new sqlconnection("server=darkover;uid=sa;pwd=password1;database=northwind"); sqldataadapter da = new sqldataadapter("select * from myimages", con); sqlcommandbuilder mycb = new sqlcommandbuilder(da); dataset ds = new dataset("myimages"); da.missingschemaaction = missingschemaaction.addwithkey; filestream fs = new filestream(@"c:\winnt\gone fishing.bmp", filemode.openorcreate, fileaccess.read); byte[] mydata= new byte[fs.length]; fs.read(mydata, 0, system.convert.toint32(fs.length)); fs.close(); da.fill(ds,"myimages"); datarow myrow; myrow=ds.tables["myimages"].newrow(); myrow["description"] = "this would be description text"; myrow["imgfield"] = mydata; ds.tables["myimages"].rows.add(myrow); da.update(ds, "myimages"); con.close(); } 雙擊 button2,然后將以下代碼添加到 button2_click 事件處理程序中: { sqlconnection con = new sqlconnection("server=darkover;uid=sa;pwd=password1;database=northwind"); sqldataadapter da = new sqldataadapter("select * from myimages", con); sqlcommandbuilder mycb = new sqlcommandbuilder(da); dataset ds = new dataset("myimages"); byte[] mydata= new byte[0]; da.fill(ds, "myimages"); datarow myrow; myrow=ds.tables["myimages"].rows[0]; mydata = (byte[])myrow["imgfield"]; int arraysize = new int(); arraysize = mydata.getupperbound(0); filestream fs = new filestream(@"c:\winnt\gone fishing2.bmp", filemode.openorcreate, fileaccess.write); fs.write(mydata, 0,arraysize); fs.close(); } 按 f5 鍵編譯并運行該應用程序。 單擊"保存到數據庫",將位于 c:\winnt\gone fishing.bmp 的圖像加載到 sql server image 字段。 單擊"保存到文件",將 sql server image 字段的數據保存回文件中。
發(fā)表者:suosuoyyy
這段代碼我側過的呀,現在你已經把image字段讀到byte[]
aaa里面呢,現在就是要根據aaa來構造bitmap嘛,你自己new一下,好像有很多種重載方法,根據要求該一下就行了,不一定要用
memorystream,好像可以直接傳byte[],自己試試!
|