相关知识:
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
代码示例:
using System.Data;using System.Data.SqlClient;using System.IO;
1 const int BUF_SIZE = 1024;//缓冲区大小2 static string strConn = @"server=Joe-PC;database=AdventureWorks_WroxSSRS2012;uid=sa;pwd=root";3 static SqlConnection conn = new SqlConnection(strConn);4 //优先把BLOB字段前的其他字段读取出来,否则一旦开始读BLOB字段,将无法再回头去读之前的字段(注意此sql语句中字段的顺序与数据库字段顺序的对比)5 static string strCmd = "SELECT ProductPhotoID,ThumbnailPhotoFileName,ThumbNailPhoto,LargePhotoFileName,LargePhoto FROM Production.ProductPhoto";6 static SqlCommand cmd = new SqlCommand(strCmd, conn);
1 static void Main(string[] args) 2 { 3 try 4 { 5 conn.Open(); 6 //以SequentialAccesss方式打开DataReader 7 SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); 8 while (dr.Read()) 9 {10 int productPhotoID = dr.GetInt32(0);11 string thumbnailPhotoFileName = dr.GetString(1);//读取缩略图文件名12 WriteThumbnailPhotoFile(thumbnailPhotoFileName, dr);//将缩略图二进制数据写入磁盘文件13 string largePhotoFileName = dr.GetString(3);//读取大图文件名14 WriteLargePhotoFile(largePhotoFileName, dr);//将大图二进制数据写入磁盘文件15 }16 }17 catch (Exception e)18 {19 Console.WriteLine(e);20 }21 finally22 {23 conn.Close();24 }25 }
1 static void WriteThumbnailPhotoFile(string fileName, SqlDataReader dr)2 {3 string path = "..//..//images//Thumbnail//" + fileName;//需要预先在项目文件夹中建立此目录4 FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);5 //ThumbnailPhoto的数据比较小(没有超过8KB),因此选择一次性读出,直接写入文件6 byte[] buf = dr[2] as byte[];7 fs.Write(buf, 0, buf.Length);8 fs.Close();9 }
1 static void WriteLargePhotoFile(string fileName, SqlDataReader dr) 2 { 3 string path = "..//..//images//Large//" + fileName;//需要预先在项目文件夹中建立此目录 4 FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); 5 byte[] buf = new byte[BUF_SIZE]; 6 long bytesRead = 0; 7 long startIndex = 0; 8 //LargePhoto的数据比较大,因此分批次读出,分别写入文件 9 while ((bytesRead = dr.GetBytes(4, startIndex, buf, 0, BUF_SIZE)) > 0)10 {11 fs.Write(buf, 0, (int)bytesRead);12 startIndex += bytesRead;13 }14 fs.Close();15 }
程序说明:
需要预先在本项目文件夹下建立images目录,内有thumbnail和large子目录,用于存放从数据库获取的图片
新闻热点
疑难解答
图片精选