首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
如何通过ADO.NET读取数据库中的图片?
如何通过ADO.NET读取数据库中的图片?
admin
2019-03-29
159
问题
如何通过ADO.NET读取数据库中的图片?
选项
答案
// Assumes that connection is a valid SqlConnection object. SqlCommand command = new SqlCommand("SELECT pub_id, logo FROM pub_info", connection); // Writes the BLOB to a file (*.bmp). FileStream stream; // Streams the BLOB to the FileStream object. BinaryWriter writer; // Size of the BLOB buffer. int bufferSize = 100; // The BLOB byte[] buffer to be filled by GetBytes. byte[] outByte = new byte[bufferSize]; // The bytes returned from GetBytes. long retval; // The starting position in the BLOB output. long startIndex = 0; // The publisher id to use in the file name. string pubID = ""; // Open the connection and read data into the DataReader. connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess); while (reader.Read()){ // Get the publisher id, which must occur before getting the logo. pubID = reader.GetString(0); // Create a file to hold the output. stream = new FileStream("logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write); writer = new BinaryWriter(stream); // Reset the starting byte for the new BLOB. startIndex = 0; // Read bytes into outByte[] and retain the number of bytes returned. retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize); // Continue while there are bytes beyond the size of the buffer. while (retval == bufferSize) { writer.Write(outByte); writer.Flush(); // Reposition start index to end of last buffer and fill buffer. startIndex += bufferSize; retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize); } // Write the remaining buffer. writer.Write(outByte, 0, (int)retval - 1); writer.Flush(); // Close the output file. writer.Close(); stream.Close(); }// Close the reader and the connection. reader.Close(); connection.Close();
解析
转载请注明原文地址:https://kaotiyun.com/show/GxmZ777K
0
程序员面试
相关试题推荐
Signslike"Pleaseratemefivestars"pointtoagrowingproblemwithbusinessesintheon-demandeconomyofapp-basedservices
删除字符串中的数字并压缩字符串(神州数码以前笔试题),如字符串”abc123de4fg56”处理后变为”abcdefg”。注意空间和效率。(下面的算法只需要一次遍历,不需要开辟新空间,时间复杂度为O(N))
C#中要使一个类支持FOREACH遍历,实现过程怎样?
公司要求开发一个继承System.Windows.Forms.ListView类的组件,要求达到以下的特殊功能:点击ListView各列列头时,能按照点击列的每行值进行重排视图中的所有行(排序的方式如DataGrid相似)。根据您的知识,请简要谈一下您的
ASP.NET能在那些系统中运行?
添加一个新的类型是计算机管理员的用户John
打开搜狐首页中的【体育】链接,将其中的一幅图片保存到“E:/Tools”文件夹,并将该网页添加到收藏夹中。
请在当前幻灯片中的图形前添加一个形状,并输入“技术部”。
从目前技术来看,下列打印机中打印速度最快的是________。
IT服务风险管理中,风险的监控是指跟踪已识别的危险,检测残余风险和识别新的风险,保证风险计划的执行,并评价这些计划对减轻风险的有效性。风险监控是整个生命周期中一个持续进行的过程。下面______不是风险监控的基本方法。
随机试题
下列关于类模板的表述中,错误的是
某商场在国庆期间组织家电促销活动,后有关部门发现其销售的部分电器中有侵犯他人商标权的产品,但该商场能证明其产品的合法来源。下列说法正确的是【】
旋覆花入煎剂应
肝脏超声检查的叙述,错误的是
自然铜的炮制方法应选用
小麦粉中哪种脂肪成分含量较高?()
下列关于会计机构的设置的表述中,正确的有()。
下列各项中,不受企业股票分割影响的有()。
在软件开发中,需求分析阶段产生的主要文档是( )。
A、Itisn’treliable.B、Itneedschecking.C、Itisdefinitelytrustworthy.D、Itwon’thurttotry.D女士最后说要告诉John,看他是否愿意试试,故D正确。A是
最新回复
(
0
)