将数据库图片写到本地硬盘上
public static bool testResponsePhoto()
{
string sSQL = "select BytesOriginal from photos";
Stream fs;
BinaryWriter bw;
int iBufferSize = 1000;
byte[] bBOLB = new byte[iBufferSize];
long lRetval;
long lStartIndex=0;
string sOutPutFileName;
Random ran=new Random(100);
string sDirPath = @"F:\personalWebSite\Upload\";
if (!Directory.Exists(sDirPath))
{
Directory.CreateDirectory(sDirPath);
}
using (SqlConnection con = SQLDB.CreateCon())
{
using (SqlCommand cmd = new SqlCommand("", con))
{
cmd.CommandText = sSQL;
try
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (sdr.Read())
{
sOutPutFileName = ran.Next(999).ToString() + DateTime.Now.ToLongDateString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()+".jpg";
fs = new FileStream(sDirPath+sOutPutFileName, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
lRetval = sdr.GetBytes(0, lStartIndex, bBOLB, 0, iBufferSize);
while (lRetval == iBufferSize)
{
bw.Write(bBOLB);
bw.Flush();
lStartIndex = lStartIndex + iBufferSize;
lRetval = sdr.GetBytes(0, lStartIndex, bBOLB, 0, iBufferSize);
}
bw.Write(bBOLB);
bw.Flush();
bw.Close();
fs.Close();
lStartIndex = 0;
}
}
return true;
}
catch
{
return false;
}
}
}
}
{
string sSQL = "select BytesOriginal from photos";
Stream fs;
BinaryWriter bw;
int iBufferSize = 1000;
byte[] bBOLB = new byte[iBufferSize];
long lRetval;
long lStartIndex=0;
string sOutPutFileName;
Random ran=new Random(100);
string sDirPath = @"F:\personalWebSite\Upload\";
if (!Directory.Exists(sDirPath))
{
Directory.CreateDirectory(sDirPath);
}
using (SqlConnection con = SQLDB.CreateCon())
{
using (SqlCommand cmd = new SqlCommand("", con))
{
cmd.CommandText = sSQL;
try
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (sdr.Read())
{
sOutPutFileName = ran.Next(999).ToString() + DateTime.Now.ToLongDateString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()+".jpg";
fs = new FileStream(sDirPath+sOutPutFileName, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
lRetval = sdr.GetBytes(0, lStartIndex, bBOLB, 0, iBufferSize);
while (lRetval == iBufferSize)
{
bw.Write(bBOLB);
bw.Flush();
lStartIndex = lStartIndex + iBufferSize;
lRetval = sdr.GetBytes(0, lStartIndex, bBOLB, 0, iBufferSize);
}
bw.Write(bBOLB);
bw.Flush();
bw.Close();
fs.Close();
lStartIndex = 0;
}
}
return true;
}
catch
{
return false;
}
}
}
}
浙公网安备 33010602011771号