【求助各位大神】image字段如何还原成文件(已知文件格式)?
//判断是否有行选中
if (DgvData.CurrentRow == null)
{
return;
}
//记录选中行
DataGridViewRow row = DgvData.CurrentRow;
SaveFileDialog saveFileDialog = new SaveFileDialog();
//默认保存到E盘
saveFileDialog.InitialDirectory = "E:\\";
saveFileDialog.FileName = row.Cells["文件名"].Value.ToString();
saveFileDialog.Filter = "Files(*." + row.Cells["扩展名"].Value.ToString()+")|*." + row.Cells["扩展名"].Value.ToString();
DialogResult drResult = saveFileDialog.ShowDialog();
//判断是否按下确定
if (drResult != DialogResult.OK)
{
return;
}
//连接数据库
string sConnection = "Server = WIN-1JUUPA83A27\\SQL2005; DataBase = DataManagement; User ID = sa; Password = sa;";
SqlConnection sqlCon = new SqlConnection(sConnection);
sqlCon.Open();
string sSelect = "select 保存的文件 from FileInformation where 文件名 = '" + row.Cells["文件名"].Value.ToString() +"'";
SqlCommand sqlCmd = new SqlCommand(sSelect, sqlCon);
MemoryStream ms = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, sqlCmd.ExecuteScalar());
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
ms.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
sqlCon.Close();
------解决方案--------------------
举个例子
File.WriteAllBytes(@"C:\a.jpg", bytes);