首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
如何通过ADO.NET读取数据库中的图片?
如何通过ADO.NET读取数据库中的图片?
admin
2019-03-29
113
问题
如何通过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
程序员面试
相关试题推荐
C#中要使一个类支持FOREACH遍历,实现过程怎样?
数据库的优化设计?
设置DOS窗口为“快速编辑模式”。
以下关于输入法状态切换的组合键正确的是______A.使用Ctrl+、来切换中文标点和英文标点B.使用Ctrl+空格键来打开或关闭中文输入法C.使用Shift+空格键来切换半角输入模式和全角输入模式D.使用Shift+Ctrl来切换半角输入模式和
在“幻灯片浏览视图”模式下,不允许进行的操作是()A.幻灯片移动和复制B.幻灯片切换C.幻灯片删除D.设置动画效果
在PPoint中,被选中对象虚框上的8个小方框称为()。A.尺寸控制点B.文本插入点C.有效区域范围D.选中对象标记
请在当前幻灯片中的图形前添加一个形状,并输入“技术部”。
将E-R图转换到关系模式时,实体与联系都可以表示成______。
下列关于通信技术的叙述中,错误的是________。
随机试题
Ifyouarewalkingawayfromaclocktower,youwillhearthetickingoftheclockfadetothepoint________itcannotbeheard.
内镜技术用于腹腔脏器探查并称为"腹腔镜检查"始于哪一年
重症肌无力因肺部感染给予相应治疗。3天后发生危象,这时首先应
下列不宜选择氟喹诺酮的是
行政赔偿义务机关与行政侵权行为人
连续源源强以单位()内排放的物质或体积表示。
Heisanartistin______butnotinreality.
将E-R图转换到关系模式时,实体与联系都可以表示成______。
不考虑异常情况,编写程序从用户处获得一个浮点数输入,如果用户输入不符合,则要求用户再次输入,直至满足条件。打印输出这个输入。
MostmusiciansagreethatthebestviolinsweremadeinCremona,Italy,about200yearsago.Theseviolinssoundbetterthana
最新回复
(
0
)