/// <summary>
/// 通过ID得到图片的MemoryStream,可以直接显示在图片框里
/// </summary>
/// <returns></returns>
public static MemoryStream ID2FileStream(int id)
{
MemoryStream rtn = null; try
{
string strSQL = "select filedata from t_filebase where id = " + id.ToString(); SqlConnection db = new SqlConnection(strConn);
db.Open();
SqlCommand cmd = new SqlCommand(strSQL, db);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if (!reader.Read())
{
rtn = null;
}
else
{ Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(reader["FileData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
rtn = stmBLOBData;
} reader.Close();
cmd.Dispose();
db.Close();
db.Dispose(); return rtn;
}
catch
{
return null;
}
} /// <summary>
/// ID -> File
/// </summary>
/// <param name="id"></param>
/// <param name="file"></param>
/// <returns></returns>
public static bool ID2File(int id,string file)
{
bool rtn = false; try
{
FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write); MemoryStream ms = new MemoryStream();
ms = ID2FileStream(id); if (ms == null)
{
rtn = false;
}
else
{ byte[] data = new byte[ms.Length]; ms.Read(data, 0, data.Length); fs.Write(data, 0, (int)data.Length);
fs.Close(); rtn = true;
} ms.Close();
ms.Dispose();
}
catch
{
rtn = false;
}
return rtn;
}
/// 通过ID得到图片的MemoryStream,可以直接显示在图片框里
/// </summary>
/// <returns></returns>
public static MemoryStream ID2FileStream(int id)
{
MemoryStream rtn = null; try
{
string strSQL = "select filedata from t_filebase where id = " + id.ToString(); SqlConnection db = new SqlConnection(strConn);
db.Open();
SqlCommand cmd = new SqlCommand(strSQL, db);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if (!reader.Read())
{
rtn = null;
}
else
{ Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(reader["FileData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
rtn = stmBLOBData;
} reader.Close();
cmd.Dispose();
db.Close();
db.Dispose(); return rtn;
}
catch
{
return null;
}
} /// <summary>
/// ID -> File
/// </summary>
/// <param name="id"></param>
/// <param name="file"></param>
/// <returns></returns>
public static bool ID2File(int id,string file)
{
bool rtn = false; try
{
FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write); MemoryStream ms = new MemoryStream();
ms = ID2FileStream(id); if (ms == null)
{
rtn = false;
}
else
{ byte[] data = new byte[ms.Length]; ms.Read(data, 0, data.Length); fs.Write(data, 0, (int)data.Length);
fs.Close(); rtn = true;
} ms.Close();
ms.Dispose();
}
catch
{
rtn = false;
}
return rtn;
}
dbdatareader rd...
object obj=rd.getvalue(图象列);
if (obj == null)
return new byte[0];
else
try
{
byte[] buffer = (byte[])obj;
return buffer;
}
catch (Exception)
{
return new byte[0];
}
其中buffer就是图象