正 文

使用ADO.NET 和C# 处理BLOB 数据


www.7dspace.com  更新日期:2005-12-4 5:24:43  七度空间


这段代码从一个特定的数据单元(行和列的交叉点)中读取一个 BLOB 对象。注意:它(通过 Using 语句)使用了System.Data.SqlClient 和System.IO 命名空间。FileStream 类的 Write 方法被用于将 BLOB 存储到一个本地驱动器中。在这种情况下,我们知道正在处理的是文档和图像文件,所以存储它们有意义。

另外一种场景涉及到读取一个数据库记录行,这条记录中一个列是 BLOB 类型而其它列不是。SqlDataReader 对象的默认行为是,只要可以得到整行数据,就将传入数据以一行记录装载。BLOB 对象的大小使这个方法不兼容,所以它们很难对待。这可以通过SqlCommand 类的ExecuteReader 方法来完成。你可以将CommandBehavior.SequentialAccess 传递给ExecuteReader 方法去修改SqlDataReader 的默认行为,以取代装载成行的数据,它将按其接收数据的顺序装载数据。

使用SequentialAccess 的主要方面是知道字段被访问的顺序。SqlDataReader 的默认行为是允许你访问你想要访问的任何字段,但是SequentialAccess 使你以SqlDataReader 对象返回的顺序访问字段。数据被顺序读取,所以数据在被读取之后,它将不再可用。

在访问 BLOB 字段的数据时,SqlDataReader 类的GetBytes、GetString 和 GetChars 方法可以用来检索数据。GetBytes 和 GetChars 允许你使用 BLOB 字段的数据填充一个数组(它们都返回 long 型数值)。下面的代码展示了它们的使用方法。

SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader sdr = null;
FileStream fs = null;
BinaryWriter bw = null;
long blob;
byte[] outBuffer = new byte[255];
const string sConn = "server=(local);Initial
Catalog=Northwind;UID=ctester;PWD=password";
try {
conn = new SqlConnection(sConn);
cmd = new SqlCommand("SELECT CategoryID, Picture FROM Categories WHERE
CategoryName='Builder'",conn);
conn.Open();
sdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (sdr.Read()) {
id = sdr.getString(0);
fs = new FileStream("c:\\Builder.doc", FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
blob = sdr.GetBytes(1, startIndex, outBuffer, 0, 255);
while (blob == 255) {
bw.Write(outBuffer);
bw.Flush();
startIndex += 255;
bw.Flush();
bw.Close();
fs.Close();
}
sdr.Close();
conn.Close();
} }

3页,页码:[1] [2] [3] 

上一篇:使用ASP.NET Global.asax 文件
下一篇:使用更安全的C字符串操纵
作者:Tony Patton  来源:开发者在线 ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐