首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
如何通过ADO.NET读取数据库中的图片?
如何通过ADO.NET读取数据库中的图片?
admin
2019-03-29
137
问题
如何通过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
ForAmerica’schildrentheeducationsystemisoftenliterallyalottery.ThatisthemainmessageofanewdocumentaryaboutAm
"Thecatdoesnotofferservices,"WilliamBurroughswrote."Thecatoffersitself."Butitdoessowithunapologeticcontradict
列举ADO.NET中的五个主要对象,并简单描述
存储过程和函数的区别
把个人的信息进行设置,显示图片“火箭发射”,与其他人共享网络摄像机功能。
从当前界面开始,对“C:\我的图片”文件夹设置共享。
启动操作系统自带的Intemet连接防火墙。
将桌面背景设置为图片WindowsXP。
请对工作簿Book1设置密码123456,同时对其结构进行保护。
随机试题
配位反应都能用于滴定分析。()
患儿,1岁,发热、咳喘6天,一天来呼吸困难。查体:患儿精神差,呼吸困难,听诊右肺呼吸音消失,叩诊右胸上方过清音,下方浊音为帮助确诊,最重要的检查是
我国外债管理的政策规定有()。
《建筑法》及相关法规规定,禁止分包单位将其承包的工程再分包,但下列分包中的()例外。
除法律法规另有规定外,银行无权在未经存款人授权或委托的情况下,擅自动用存款人在银行账户里的资金,这体现的办理支付结算的原则是()。
曾指出“大学者,非谓有大楼之谓也,有大师之谓也”的是()。
刑事强制权是为了保证刑事诉讼的顺利进行,由公安机关对()行使的强制权力。
巴拉圭战争
TheWhiteHouseandotherfederalbuildingswere______asaplaneenterstherestrictedairspace.
ThePlaceWherethePoorOnceThrivedA)Thisisthelandofopportunity.Ifthatweren’talreadyimpliedbythelandscape—ro
最新回复
(
0
)