本人試圖使用如下代碼取出sql數(shù)據(jù)庫(kù)中的image類型的數(shù)據(jù),
但是提示“指定的轉(zhuǎn)換無(wú)效”。
1、請(qǐng)問(wèn)如何取出sql數(shù)據(jù)庫(kù)中的image類型的數(shù)據(jù)?
2、請(qǐng)問(wèn)sql數(shù)據(jù)庫(kù)中的image類型存放的是不是圖片在外存的絕對(duì)地址?
image類型數(shù)據(jù)的真正含義是什么?
感謝您的回復(fù)?。?
--
//打開(kāi)連接
con.open();
string sql="select id,img from test where id="+convert.toint32(this.textbox1.text)+"";
da2=new sqldataadapter(sql,con);
//用相關(guān)的數(shù)據(jù)填充數(shù)據(jù)容器dataset
da2.fill(ds,"ds2");
this.picturebox2.image = (system.drawing.image)ds.tables["ds2"].rows[0][1];
con.close();
how to:在 visual c# 中直接將一個(gè)圖片從數(shù)據(jù)庫(kù)復(fù)制到 picturebox 控件
適用于
有關(guān)本文的 microsoft visual basic .net 版本,請(qǐng)參閱 317670。
本任務(wù)的內(nèi)容
摘要
要求
示例
缺陷
參考
概要
本分步指南介紹如何將存儲(chǔ)在數(shù)據(jù)庫(kù)中的圖像直接復(fù)制到 windows 窗體上的 picturebox 控件,而無(wú)須將此圖像保存到文件。
在 microsoft visual basic 6.0 中,如想直接在 picturebox 控件中顯示數(shù)據(jù)庫(kù)中的圖像而不經(jīng)過(guò)將二進(jìn)制大對(duì)象 {blob) 數(shù)據(jù)保存到文件這一中間步驟,唯一的方法是將 picturebox 綁定到一個(gè)數(shù)據(jù)源,如 activex data objects (ado) 數(shù)據(jù)控件或記錄集。若不將圖像保存到文件以供 loadpicture 語(yǔ)句使用,則沒(méi)有辦法以編程方式將 blob 加載到控件。
在本文中,我們將使用 system.io 基類中的 memorystream 對(duì)象將圖像數(shù)據(jù)直接從數(shù)據(jù)庫(kù)復(fù)制到 picturebox 控件。
返回頁(yè)首
要求
下表概括了推薦使用的硬件、軟件、網(wǎng)絡(luò)結(jié)構(gòu)以及所需的 service pack:
安裝在兼容 microsoft windows 操作系統(tǒng)中的 microsoft visual studio .net
用于測(cè)試的可用 microsoft sql server 實(shí)例或可用 microsoft access 數(shù)據(jù)庫(kù)
本文假定您熟悉下列主題:
visual c# .net windows 窗體應(yīng)用程序
數(shù)據(jù)庫(kù)中的二進(jìn)制大對(duì)象 (blob) 存儲(chǔ)
ado.net 數(shù)據(jù)訪問(wèn)
返回頁(yè)首
示例
使用以下結(jié)構(gòu)創(chuàng)建一個(gè) sql server 或 access 表:create table blobtest
(
blobid int identity not null,
blobdata image not null
)
打開(kāi) visual studio .net,然后新建一個(gè) visual c# windows 應(yīng)用程序項(xiàng)目。
從工具箱向默認(rèn)的 form1 添加一個(gè) picturebox 和兩個(gè) button 控件。將 button1 的 text 屬性設(shè)置為 file to database,并將 button2 的 text 屬性設(shè)置為 database to picturebox。
在窗體的代碼模塊頂部插入 using 語(yǔ)句:using system.data.sqlclient;
using system.io;
using system.drawing.imaging;
將以下數(shù)據(jù)庫(kù)連接字符串的聲明添加到 public class form1 :system.windows.forms.form 類聲明中,并根據(jù)需要調(diào)整連接字符串: string strcn = "data source=localhost;integrated security=sspi;initial catalog=mydata";
將下面的代碼插入 button1 (file to database) 的 click 事件過(guò)程中。根據(jù)需要調(diào)整到一個(gè)可用示例圖像文件的可用路徑。此代碼可將圖像文件從磁盤(pán)讀入 byte 數(shù)組,然后使用一個(gè)參數(shù)化的 command 對(duì)象將數(shù)據(jù)插入數(shù)據(jù)庫(kù)。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 事件過(guò)程。此代碼將行從數(shù)據(jù)庫(kù)中的 blobtest 表檢索到一個(gè)數(shù)據(jù)集,復(fù)制最新添加的圖像到 byte 數(shù)組,然后到 memorystream 對(duì)象,接著將 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 鍵編譯并運(yùn)行該項(xiàng)目。
單擊 file to database 按鈕將至少一個(gè)示例圖像加載到數(shù)據(jù)庫(kù)。
單擊 database to picturebox 按鈕將保存的圖像顯示在 picturebox 控件中。
如果想能夠直接將圖像從 picturebox 控件插入數(shù)據(jù)庫(kù),則請(qǐng)?zhí)砑拥谌齻€(gè) button 控件,并將下面的代碼插入其 click 事件過(guò)程。此代碼將圖像數(shù)據(jù)從 picturebox 控件檢索到 memorystream 對(duì)象,將 memorystream 復(fù)制到一個(gè) byte 數(shù)組,然后使用一個(gè)參數(shù)化的 command 對(duì)象將 byte 數(shù)組保存到數(shù)據(jù)庫(kù)。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);}
運(yùn)行該項(xiàng)目。單擊 database to picturebox 按鈕以顯示剛才在 picturebox 控件中保存過(guò)的圖像。單擊新添加的按鈕將此圖像從 picturebox 保存到數(shù)據(jù)庫(kù)。然后再次單擊 database to picturebox 按鈕以確認(rèn)圖像已正確保存。
返回頁(yè)首
缺陷
此測(cè)試不適用于 access 和 sql server 中的羅斯文示例數(shù)據(jù)庫(kù)的雇員表中的照片列。存儲(chǔ)在照片列中的位圖圖像用由 visual basic 6.0 ole container 控件創(chuàng)建的標(biāo)題信息進(jìn)行了包裝。
如果需要使用 access 數(shù)據(jù)庫(kù)測(cè)試此代碼,則需要在 access 表中創(chuàng)建一個(gè) ole object 類型的列,并使用 microsoft jet 4.0 provider 中的 system.data.oledb 名稱空間代替 system.data.sqlclient 名稱空間。
how to:在 visual c# .net 中通過(guò)使用 ado.net 讀寫(xiě) blob 數(shù)據(jù)
適用于
本文的發(fā)布號(hào)曾為 chs309158
有關(guān)本文的 microsoft visual basic .net 版本,請(qǐng)參見(jiàn) 308042。
有關(guān)本文的 microsoft visual j# .net 版本,請(qǐng)參見(jiàn) 320629。
本文引用下面的 microsoft .net 框架類庫(kù)名稱空間:
system.data.sqlclient
system.io
本任務(wù)的內(nèi)容
概要
要求
創(chuàng)建項(xiàng)目
概要
在 ado.net 中,datareader 列、dataset 列或 command 參數(shù)不能使用 getchunk 和 appendchunk 方法。本文介紹如何使用 visual c# .net 讀寫(xiě)二進(jìn)制大對(duì)象 (blob) 字段。
返回頁(yè)首
要求
下面的列表列出了推薦使用的硬件、軟件、網(wǎng)絡(luò)結(jié)構(gòu)以及所需的 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
返回頁(yè)首
創(chuàng)建項(xiàng)目
在您的 sql server 羅斯文數(shù)據(jù)庫(kù)中添加一個(gè)名為 myimages 的表。在該表中包含以下字段:
標(biāo)識(shí)字段,名為"id",類型為 int。
字段,名為"description",類型為 varchar,長(zhǎng)度為 50。
字段,名為"imgfield",類型為 image。
啟動(dòng) visual studio .net,然后新建一個(gè) visual c# windows 應(yīng)用程序項(xiàng)目。
將兩個(gè) button 控件從工具箱拖到默認(rèn)窗體 form1 上。
在"屬性"窗口中,將 button1 的 text 屬性更改為保存到數(shù)據(jù)庫(kù),將 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 鍵編譯并運(yùn)行該應(yīng)用程序。
單擊"保存到數(shù)據(jù)庫(kù)",將位于 c:\winnt\gone fishing.bmp 的圖像加載到 sql server image 字段。
單擊"保存到文件",將 sql server image 字段的數(shù)據(jù)保存回文件中。
這段代碼我側(cè)過(guò)的呀,現(xiàn)在你已經(jīng)把image字段讀到byte[] aaa里面呢,現(xiàn)在就是要根據(jù)aaa來(lái)構(gòu)造bitmap嘛,你自己new一下,好像有很多種重載方法,根據(jù)要求該一下就行了,不一定要用memorystream,好像可以直接傳byte[],自己試試!
聯(lián)系客服