代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
大型对象BLOB用法
作者:
Dezai.CN
/ 发布于
2011/6/22
/
441
<div><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 从数据库读取Lob到文件 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> ReadLobToFile() { </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeeIdColumn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeePhotoColumn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferSize </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[bufferSize]; </span><span style="color: #0000ff">int</span><span style="color: #000000"> byteCountRead; </span><span style="color: #0000ff">long</span><span style="color: #000000"> currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; ConnectionStringSettings nwSetting </span><span style="color: #000000">=</span><span style="color: #000000"> ConfigurationManager.ConnectionStrings[</span><span style="color: #800000">"</span><span style="color: #800000">NorthWindDb</span><span style="color: #800000">"</span><span style="color: #000000">]; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlConnection cn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection()) { cn.ConnectionString </span><span style="color: #000000">=</span><span style="color: #000000"> nwSetting.ConnectionString; cn.Open(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">Select EmployeeID,Photo From Employees</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008000">/*</span><span style="color: #008000">SequentialAccess 不是加载整行,而是使 DataReader 将数据作为流来加载。 然后可以使用 GetBytes 或 GetChars 方法来指定开始读取操作的字节位置以及正在返回的数据的有限的缓冲区大小。 </span><span style="color: #008000">*/</span><span style="color: #000000"> SqlDataReader rdr </span><span style="color: #000000">=</span><span style="color: #000000"> cmd.ExecuteReader(CommandBehavior.SequentialAccess); </span><span style="color: #0000ff">while</span><span style="color: #000000"> (rdr.Read()) { </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeeId </span><span style="color: #000000">=</span><span style="color: #000000"> rdr.GetInt32(employeeIdColumn); </span><span style="color: #0000ff">string</span><span style="color: #000000"> fileName </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">@"</span><span style="color: #800000">D:\Employee</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> employeeId.ToString().PadLeft(</span><span style="color: #800080">2</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">0</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">.bin</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (FileStream fs </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)) { currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">从指定的列偏移量将字节流读入缓冲区,并将其作为从给定的缓冲区偏移量开始的数组。 </span><span style="color: #008000"> </span><span style="color: #008000">/*</span><span style="color: #008000"> 参数 i 类型:Int32 从零开始的列序号。 dataIndex 类型:Int64 字段中的索引,从其开始读取操作。 buffer 类型:array<Byte>[]()[] 要将字节流读入的缓冲区。 bufferIndex 类型:Int32 buffer 中写入操作开始位置的索引。 length 类型:Int32 要复制到缓冲区中的最大长度。 </span><span style="color: #008000">*/</span><span style="color: #000000"> byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)rdr.GetBytes(employeePhotoColumn, currentIndex, buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, bufferSize); </span><span style="color: #0000ff">while</span><span style="color: #000000"> (byteCountRead </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { fs.Write(buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, byteCountRead); currentIndex </span><span style="color: #000000">+=</span><span style="color: #000000"> byteCountRead; byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)rdr.GetBytes(employeePhotoColumn, currentIndex, buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, bufferSize); } } } } } }</span> <div style="width: 900px" class="cnblogs_code"> <div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 得到头信息及图片 </span><span style="color: #808080">///</span><span style="color: #008000"> 该代码检查该照片是否包含OLE头信息。如果包含将从OLE头信息后面开始执行写操作。 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> GetHeadInfo() { </span><span style="color: #008000">//</span><span style="color: #008000">OLE头信息大小为78</span><span style="color: #008000"> </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> oleOffset </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">78</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">扩展名开始</span><span style="color: #008000"> </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> oleTypeStart </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">20</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">扩展名占用长度</span><span style="color: #008000"> </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> oleTypeLength </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">12</span><span style="color: #000000">; </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeeIdColumn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeePhotoColumn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferSize </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">must be bigger than oleOffset</span><span style="color: #008000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[bufferSize]; </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferStart </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">int</span><span style="color: #000000"> byteCountRead; </span><span style="color: #0000ff">long</span><span style="color: #000000"> currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; ConnectionStringSettings nwSetting </span><span style="color: #000000">=</span><span style="color: #000000"> ConfigurationManager.ConnectionStrings[</span><span style="color: #800000">"</span><span style="color: #800000">NorthWindDb</span><span style="color: #800000">"</span><span style="color: #000000">]; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlConnection cn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection()) { cn.ConnectionString </span><span style="color: #000000">=</span><span style="color: #000000"> nwSetting.ConnectionString; cn.Open(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">Select EmployeeID,Photo From Employees</span><span style="color: #800000">"</span><span style="color: #000000">; SqlDataReader rdr </span><span style="color: #000000">=</span><span style="color: #000000"> cmd.ExecuteReader(CommandBehavior.SequentialAccess); </span><span style="color: #0000ff">while</span><span style="color: #000000"> (rdr.Read()) { </span><span style="color: #0000ff">int</span><span style="color: #000000"> employeeId </span><span style="color: #000000">=</span><span style="color: #000000"> rdr.GetInt32(employeeIdColumn); </span><span style="color: #008000">//</span><span style="color: #008000">文件路径</span><span style="color: #008000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> fileName </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">@"</span><span style="color: #800000">D:\Employee\Employee</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> employeeId.ToString().PadLeft(</span><span style="color: #800080">2</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">0</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">.bin</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (FileStream fs </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)) { currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">while</span><span style="color: #000000"> (currentIndex </span><span style="color: #000000"><</span><span style="color: #000000"> oleOffset) { </span><span style="color: #008000">//</span><span style="color: #008000">第一批次读取</span><span style="color: #008000"> </span><span style="color: #000000">byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)rdr.GetBytes(employeePhotoColumn, currentIndex, buffer, (</span><span style="color: #0000ff">int</span><span style="color: #000000">)currentIndex, bufferSize </span><span style="color: #000000">-</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)currentIndex); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (byteCountRead </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { </span><span style="color: #0000ff">break</span><span style="color: #000000">; } currentIndex </span><span style="color: #000000">+=</span><span style="color: #000000"> byteCountRead; } byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)currentIndex; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (byteCountRead </span><span style="color: #000000">>=</span><span style="color: #000000"> oleOffset) { </span><span style="color: #008000">//</span><span style="color: #008000">得到文件类型</span><span style="color: #008000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> type </span><span style="color: #000000">=</span><span style="color: #000000"> Encoding.ASCII.GetString(buffer, oleTypeStart, oleTypeLength); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (type </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">Bitmap Image</span><span style="color: #800000">"</span><span style="color: #000000">) { </span><span style="color: #008000">//</span><span style="color: #008000">第一批次,从头信息后开使读取</span><span style="color: #008000"> </span><span style="color: #000000">bufferStart </span><span style="color: #000000">=</span><span style="color: #000000"> oleOffset; </span><span style="color: #008000">//</span><span style="color: #008000">第一批次的读取大小</span><span style="color: #008000"> </span><span style="color: #000000">byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> byteCountRead </span><span style="color: #000000">-</span><span style="color: #000000"> oleOffset; } } </span><span style="color: #008000">//</span><span style="color: #008000">写入文件</span><span style="color: #008000"> </span><span style="color: #0000ff">while</span><span style="color: #000000"> (byteCountRead </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { fs.Write(buffer, bufferStart, byteCountRead); bufferStart </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; byteCountRead </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000">)rdr.GetBytes(employeePhotoColumn, currentIndex, buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, bufferSize); currentIndex </span><span style="color: #000000">+=</span><span style="color: #000000"> byteCountRead; } } } } } }</span></div> </div> <div style="width: 900px" class="cnblogs_code"> <div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 将图片写入到数据库 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> WriteBlobToDb() { </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferSize </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[bufferSize]; </span><span style="color: #0000ff">long</span><span style="color: #000000"> currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] photoPtr; ConnectionStringSettings nwSetting </span><span style="color: #000000">=</span><span style="color: #000000"> ConfigurationManager.ConnectionStrings[</span><span style="color: #800000">"</span><span style="color: #800000">NorthWindDb</span><span style="color: #800000">"</span><span style="color: #000000">]; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlConnection cn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection()) { cn.ConnectionString </span><span style="color: #000000">=</span><span style="color: #000000"> nwSetting.ConnectionString; cn.Open(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">select Textptr(Photo) from employees where employeeID='1'</span><span style="color: #800000">"</span><span style="color: #000000">; photoPtr </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">byte</span><span style="color: #000000">[])cmd.ExecuteScalar(); } </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">UpdateText employees.Photo @Pointer @Offset null @Data</span><span style="color: #800000">"</span><span style="color: #000000">; SqlParameter ptrParm </span><span style="color: #000000">=</span><span style="color: #000000"> cmd.Parameters.Add(</span><span style="color: #800000">"</span><span style="color: #800000">Pointer</span><span style="color: #800000">"</span><span style="color: #000000">, SqlDbType.Binary, </span><span style="color: #800080">16</span><span style="color: #000000">); ptrParm.Value </span><span style="color: #000000">=</span><span style="color: #000000"> photoPtr; SqlParameter PhotoParm </span><span style="color: #000000">=</span><span style="color: #000000"> cmd.Parameters.Add(</span><span style="color: #800000">"</span><span style="color: #800000">Data</span><span style="color: #800000">"</span><span style="color: #000000">, SqlDbType.Image); SqlParameter offsetParm </span><span style="color: #000000">=</span><span style="color: #000000"> cmd.Parameters.Add(</span><span style="color: #800000">"</span><span style="color: #800000">Offset</span><span style="color: #800000">"</span><span style="color: #000000">, SqlDbType.Int); offsetParm.Value </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (FileStream fs </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileStream(</span><span style="color: #800000">"</span><span style="color: #800000">Girl.png</span><span style="color: #800000">"</span><span style="color: #000000">, FileMode.Open, FileAccess.Read)) { </span><span style="color: #0000ff">int</span><span style="color: #000000"> count </span><span style="color: #000000">=</span><span style="color: #000000"> fs.Read(buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, bufferSize); </span><span style="color: #0000ff">while</span><span style="color: #000000"> (count </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { PhotoParm.Value </span><span style="color: #000000">=</span><span style="color: #000000"> buffer; PhotoParm.Size </span><span style="color: #000000">=</span><span style="color: #000000"> count; cmd.ExecuteNonQuery(); currentIndex </span><span style="color: #000000">+=</span><span style="color: #000000"> count; offsetParm.Value </span><span style="color: #000000">=</span><span style="color: #000000"> currentIndex; count </span><span style="color: #000000">=</span><span style="color: #000000"> fs.Read(buffer, </span><span style="color: #800080">0</span><span style="color: #000000">, bufferSize); } } } } MessageBox.Show(</span><span style="color: #800000">"</span><span style="color: #800000">Done</span><span style="color: #800000">"</span><span style="color: #000000">); }</span></div> </div> <div style="width: 900px" class="cnblogs_code"> <div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 显示有头信息的图片 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> ShowImgHaveHead() { </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferSize </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[bufferSize]; </span><span style="color: #0000ff">long</span><span style="color: #000000"> currentIndex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] OleArray; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] photoPtr; ConnectionStringSettings nwSetting </span><span style="color: #000000">=</span><span style="color: #000000"> ConfigurationManager.ConnectionStrings[</span><span style="color: #800000">"</span><span style="color: #800000">NorthWindDb</span><span style="color: #800000">"</span><span style="color: #000000">]; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlConnection cn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection()) { cn.ConnectionString </span><span style="color: #000000">=</span><span style="color: #000000"> nwSetting.ConnectionString; cn.Open(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">select Photo from employees where employeeID='2'</span><span style="color: #800000">"</span><span style="color: #000000">; OleArray </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">byte</span><span style="color: #000000">[])cmd.ExecuteScalar(); photoPtr </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[OleArray.Length </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">78</span><span style="color: #000000">]; </span><span style="color: #008000">//</span><span style="color: #008000">去除头信息(如此BLob中前78个字节为头信息)</span><span style="color: #008000"> </span><span style="color: #000000">Array.Copy(OleArray, </span><span style="color: #800080">78</span><span style="color: #000000">, photoPtr, </span><span style="color: #800080">0</span><span style="color: #000000">, (OleArray.Length </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">78</span><span style="color: #000000">)); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (MemoryStream ms </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> MemoryStream(photoPtr)) { Bitmap bmp </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Bitmap(ms); ptbImg.Image </span><span style="color: #000000">=</span><span style="color: #000000"> bmp; } } } MessageBox.Show(</span><span style="color: #800000">"</span><span style="color: #800000">Done</span><span style="color: #800000">"</span><span style="color: #000000">); }</span></div> </div> <div style="width: 900px" class="cnblogs_code"> <div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 显示没有头文信息的图片 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> ShowImgNoHead() { </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> bufferSize </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[bufferSize]; </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] photoPtr; ConnectionStringSettings nwSetting </span><span style="color: #000000">=</span><span style="color: #000000"> ConfigurationManager.ConnectionStrings[</span><span style="color: #800000">"</span><span style="color: #800000">NorthWindDb</span><span style="color: #800000">"</span><span style="color: #000000">]; </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlConnection cn </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> SqlConnection()) { cn.ConnectionString </span><span style="color: #000000">=</span><span style="color: #000000"> nwSetting.ConnectionString; cn.Open(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (SqlCommand cmd </span><span style="color: #000000">=</span><span style="color: #000000"> cn.CreateCommand()) { cmd.CommandText </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">select Photo from employees where employeeID='2'</span><span style="color: #800000">"</span><span style="color: #000000">; photoPtr </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">byte</span><span style="color: #000000">[])cmd.ExecuteScalar(); </span><span style="color: #0000ff">using</span><span style="color: #000000"> (MemoryStream ms </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> MemoryStream(photoPtr)) { Bitmap bmp </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Bitmap(ms); ptbImg.Image </span><span style="color: #000000">=</span><span style="color: #000000"> bmp; } } } MessageBox.Show(</span><span style="color: #800000">"</span><span style="color: #800000">Done</span><span style="color: #800000">"</span><span style="color: #000000">); }</span></div> </div> </div>
试试其它关键字
BLOB用法
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3