首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
如何通过ADO.NET读取数据库中的图片?
如何通过ADO.NET读取数据库中的图片?
admin
2019-03-29
122
问题
如何通过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
程序员面试
相关试题推荐
Theimmunesystemisequalincomplexitytothecombinedintricaciesofthebrainandnervoussystem.Thesuccessoftheimmune
TruthinadvertisingisaconceptcentraltotheAmericanfreemarketeconomicsystem.Accordingtothistheory,companiesthat
为系统创建一个无毒点,以便遭遇病毒时将系统还原。
在PPoint中,对于艺术字的用法,以下说法不正确的是()。A.艺术字是作为文本对象处理的B.艺术字是作为图形对象处理的C.艺术字有多种式样和字体字号D.艺术字可整体缩放
Dreamweaver的视图(View)菜单中,Grid→settings表示______。A.选择计量系统来设定标尺单位B.显示一个应用当前设置的背景网格C.使插入对象与最近的对齐设置对齐D.进行网格设置
在PPoint中,被选中对象虚框上的8个小方框称为()。A.尺寸控制点B.文本插入点C.有效区域范围D.选中对象标记
在foxpro中定义数据库结构时,字段名的宽度最多可以是()。A.2B.4C.10D.16
数据库系统的三级模式分别为()模式、内部级模式与外部级模式。
IT服务风险管理中,风险的监控是指跟踪已识别的危险,检测残余风险和识别新的风险,保证风险计划的执行,并评价这些计划对减轻风险的有效性。风险监控是整个生命周期中一个持续进行的过程。下面______不是风险监控的基本方法。
随机试题
鼻咽癌就诊时上颈部有转移淋巴结发生的几率约为
角膜软化症、干眼症、夜盲症凝血酶原过低症、维生素K缺乏症及新生儿出血症
以子实体入药的中药是
川芎为乳香为
设计方项目管理的目标不包括()。
辽宁本溪的红海滩是世界上最大的湿地红海滩奇观。()
下列不属于拉丁美洲音乐的是()。
RS技术广泛用于资源调查、环境监测、自然灾害防御监测等领域。()
设有定义:doublea,b,c;若要求通过输入分别给a、b、c输入1、2、3,输入形式如下(注:此处□代表一个空格)□□1.0□□2.0□□3.0则能进行正确输入的语句是
A、Invitethewaitertoafancydinner.B、TellherstorytotheDailyNews.C、Givesomemoneytothewaiter.D、Paythewaiter’ss
最新回复
(
0
)