如何读取Sql Server中image类型并保存为本地文件? 如题,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //读取pubs数据库logo字段using System;using System.IO;using System.Data;using System.Data.SqlClient;class BLOBDemo{ [STAThread] static void Main(string[] args) { SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=pubs"); SqlCommand cmd = new SqlCommand("Select pub_id,logo FROM pub_info", cn); FileStream fs; BinaryWriter bw; //缓冲区大小 const int bufferSize = 100; byte [] outByte = new byte[bufferSize]; //GetBytes返回的字节数量 long retval; //BLOB输出的起始位置 long startIndex = 0; string pub_id = ""; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read()) { pub_id = dr.GetString(0); fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); startIndex = 0; do { retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize); // Console.WriteLine(retval.ToString()); bw.Write(outByte); bw.Flush(); startIndex += bufferSize; }while(retval == bufferSize); bw.Write(outByte, 0, (int)retval - 1); bw.Flush(); bw.Close(); fs.Close(); } dr.Close(); cn.Close(); }} 谢谢amandag(高歌) 的回复,我试试先。 求正则。最好写好注释 谁能推荐一款电子书制作程序? submit按钮在什么情况下没有反应? asp.net中的span 关于xsd文件无法使用的疑问? 多表单提交 我用Microsoft Visual Studio .NET编辑DataGrid时怎么没找到HyperLinkColumn属性? 数据更新 1.外键 2.对象 Asp.Net网站的触发更新(升级) asp.net与js页面通过socket通讯 petshop4有段不明白的地方,请高人指点! 用oledb连接dbf 数据库出现"外部表不是预期的格式"
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=pubs");
SqlCommand cmd = new SqlCommand("Select pub_id,logo FROM pub_info", cn); FileStream fs;
BinaryWriter bw; //缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval;
//BLOB输出的起始位置
long startIndex = 0; string pub_id = ""; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read())
{
pub_id = dr.GetString(0); fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs); startIndex = 0;
do
{
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
// Console.WriteLine(retval.ToString());
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
}while(retval == bufferSize); bw.Write(outByte, 0, (int)retval - 1);
bw.Flush(); bw.Close();
fs.Close();
} dr.Close();
cn.Close();
}
}