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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

關(guān)于用C#操作數(shù)據(jù)庫(kù)中的Image數(shù)據(jù)

2024-07-21 02:22:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這里有關(guān)于用c#讀取數(shù)據(jù)庫(kù)中的image數(shù)據(jù)的介紹,
誰(shuí)知道該怎么把image對(duì)象寫(xiě)入數(shù)據(jù)庫(kù),?以及對(duì)數(shù)據(jù)庫(kù)中image對(duì)象
進(jìn)行檢索?
---------------------------------------------------
從數(shù)據(jù)庫(kù)中獲取 blob 值

datareader 的默認(rèn)行為是在整個(gè)數(shù)據(jù)行可用時(shí)立即以行的形式加載傳入數(shù)據(jù)。但是,對(duì)于二進(jìn)制大對(duì)象 (blob) 則需要進(jìn)行不同的處理,因?yàn)樗鼈兛赡馨瑪?shù)十億字節(jié)的數(shù)據(jù),而單個(gè)行中無(wú)法包含如此多的數(shù)據(jù)。command.executereader 方法具有一個(gè)重載,它將采用 commandbehavior 參數(shù)來(lái)修改 datareader 的默認(rèn)行為。您可以將 commandbehavior.sequentialaccess 傳遞到 executereader 方法來(lái)修改 datareader 的默認(rèn)行為,以便讓 datareader 按照順序在接收到數(shù)據(jù)時(shí)立即將其加載,而不是加載數(shù)據(jù)行。這是加載 blob 或其他大數(shù)據(jù)結(jié)構(gòu)的理想方案。

在將 datareader 設(shè)置為使用 sequentialaccess 時(shí),務(wù)必要注意訪問(wèn)所返回字段的順序。datareader 的默認(rèn)行為是在整個(gè)行可用時(shí)立即加載該行,這使您能夠在讀取下一行之前按任何順序訪問(wèn)所返回的字段。但是,當(dāng)使用 sequentialaccess 時(shí),必須按順序訪問(wèn)由 datareader 返回的不同字段。例如,如果查詢返回三個(gè)列,其中第三列是 blob,則必須在訪問(wèn)第三個(gè)字段中的 blob 數(shù)據(jù)之前返回第一個(gè)和第二個(gè)字段的值。如果在訪問(wèn)第一個(gè)或第二個(gè)字段之前訪問(wèn)第三個(gè)字段,則第一個(gè)和第二個(gè)字段值將不再可用。這是因?yàn)?sequentialaccess 已修改 datareader,使其按順序返回?cái)?shù)據(jù),當(dāng) datareader 已經(jīng)讀取超過(guò)特定數(shù)據(jù)時(shí),該數(shù)據(jù)將不可用。

當(dāng)訪問(wèn) blob 字段中的數(shù)據(jù)時(shí),請(qǐng)使用 datareader 的 getbytes 類型化訪問(wèn)器,該訪問(wèn)器將使用二進(jìn)制數(shù)據(jù)填充 byte 數(shù)組。您可以指定要返回的特定數(shù)據(jù)緩沖區(qū)大小以及從返回的數(shù)據(jù)中讀取的第一個(gè)字節(jié)的起始位置。getbytes 將返回 long 值,它表示所返回的字節(jié)數(shù)。如果向 getbytes 傳遞空的 byte 數(shù)組,所返回的長(zhǎng)值將是 blob 中字節(jié)的總數(shù)。您可以選擇將字節(jié)數(shù)組中的某索引指定為所讀取數(shù)據(jù)的起始位置。

以下示例從 microsoft sql server 中的 pubs 示例數(shù)據(jù)庫(kù)中返回發(fā)行者 id 和徽標(biāo)。發(fā)行者 id (pub_id) 是字符字段,而徽標(biāo)則是圖形,即 blob。請(qǐng)注意,由于必須按順序訪問(wèn)字段,所以將在訪問(wèn)徽標(biāo)之前訪問(wèn)當(dāng)前數(shù)據(jù)行的發(fā)行者 id。

[visual basic]
dim pubsconn as sqlconnection = new sqlconnection("data source=localhost;integrated security=sspi;initial catalog=pubs;")
dim logocmd as sqlcommand = new sqlcommand("select pub_id, logo from pub_info", pubsconn)

dim fs as filestream                 ' writes the blob to a file (*.bmp).
dim bw as binarywriter               ' streams the binary data to the filestream object.

dim buffersize as integer = 100      ' the size of the blob buffer.
dim outbyte(buffersize - 1) as byte  ' the blob byte() buffer to be filled by getbytes.
dim retval as long                   ' the bytes returned from getbytes.
dim startindex as long = 0           ' the starting position in the blob output.

dim pub_id as string = ""            ' the publisher id to use in the file name.

' open the connection and read data into the datareader.
pubsconn.open()
dim myreader as sqldatareader = logocmd.executereader(commandbehavior.sequentialaccess)

do while myreader.read()
  ' get the publisher id, which must occur before getting the logo.
  pub_id = myreader.getstring(0)

  ' create a file to hold the output.
  fs = new filestream("logo" & pub_id & ".bmp", filemode.openorcreate, fileaccess.write)
  bw = new binarywriter(fs)

  ' reset the starting byte for a new blob.
  startindex = 0

  ' read bytes into outbyte() and retain the number of bytes returned.
  retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize)

  ' continue reading and writing while there are bytes beyond the size of the buffer.
  do while retval = buffersize
    bw.write(outbyte)
    bw.flush()

    ' reposition the start index to the end of the last buffer and fill the buffer.
    startindex = startindex + buffersize
    retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize)
  loop

  ' write the remaining buffer.
  bw.write(outbyte)
  bw.flush()

  ' close the output file.
  bw.close()
  fs.close()
loop

' close the reader and the connection.
myreader.close()
pubsconn.close()
[c#]
sqlconnection pubsconn = new sqlconnection("data source=localhost;integrated security=sspi;initial catalog=pubs;");
sqlcommand logocmd = new sqlcommand("select pub_id, logo from pub_info", pubsconn);

filestream fs;                          // writes the blob to a file (*.bmp).
binarywriter bw;                        // streams the blob to the filestream object.

int buffersize = 100;                   // size of the blob buffer.
byte[] outbyte = new byte[buffersize];  // the blob byte[] buffer to be filled by getbytes.
long retval;                            // the bytes returned from getbytes.
long startindex = 0;                    // the starting position in the blob output.

string pub_id = "";                     // the publisher id to use in the file name.

// open the connection and read data into the datareader.
pubsconn.open();
sqldatareader myreader = logocmd.executereader(commandbehavior.sequentialaccess);

while (myreader.read())
{
  // get the publisher id, which must occur before getting the logo.
  pub_id = myreader.getstring(0);  

  // create a file to hold the output.
  fs = new filestream("logo" + pub_id + ".bmp", filemode.openorcreate, fileaccess.write);
  bw = new binarywriter(fs);

  // reset the starting byte for the new blob.
  startindex = 0;

  // read the bytes into outbyte[] and retain the number of bytes returned.
  retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize);

  // continue reading and writing while there are bytes beyond the size of the buffer.
  while (retval == buffersize)
  {
    bw.write(outbyte);
    bw.flush();

    // reposition the start index to the end of the last buffer and fill the buffer.
    startindex+= buffersize;
    retval = myreader.getbytes(1, startindex, outbyte, 0, buffersize);
  }

  // write the remaining buffer.
  bw.write(outbyte);
  bw.flush();

  // close the output file.
  bw.close();
  fs.close();
}

// close the reader and the connection.
myreader.close();
pubsconn.close();
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 瓮安县| 安乡县| 肇庆市| 柳河县| 乌审旗| 南昌县| 河北区| 凌云县| 大姚县| 宁安市| 衡东县| 荣成市| 胶南市| 海南省| 杂多县| 墨竹工卡县| 张家界市| 德化县| 顺平县| 曲麻莱县| 天等县| 玛曲县| 察雅县| 唐海县| 仁化县| 大厂| 台中县| 庆城县| 新宾| 海门市| 宽城| 西和县| 金华市| 肇庆市| 内乡县| 综艺| 临潭县| 咸宁市| 吉木萨尔县| 中西区| 高邑县|